home *** CD-ROM | disk | FTP | other *** search
/ Risc World 7 / Risc World 7.iso / Software / Issue6 / SDL.ZIP / !gcc / docs / flex < prev    next >
Text File  |  2006-09-17  |  503KB  |  9,460 lines

  1. <html lang="en">
  2. <head>
  3. <title>flex: a fast lexical analyzer generator</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="flex: a fast lexical analyzer generator">
  6. <meta name="generator" content="makeinfo 4.8">
  7. <link title="Top" rel="top" href="#Top">
  8. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  9. <!--
  10. The flex manual is placed under the same licensing conditions as the
  11. rest of flex:
  12.  
  13. Copyright (C) 1990, 1997 The Regents of the University of California.
  14. All rights reserved.
  15.  
  16. This code is derived from software contributed to Berkeley by
  17. Vern Paxson.
  18.  
  19. The United States Government has rights in this work pursuant
  20. to contract no. DE-AC03-76SF00098 between the United States
  21. Department of Energy and the University of California.
  22.  
  23. Redistribution and use in source and binary forms, with or without
  24. modification, are permitted provided that the following conditions
  25. are met:
  26.   1.  Redistributions of source code must retain the above copyright
  27.      notice, this list of conditions and the following disclaimer.
  28.  
  29.   2. Redistributions in binary form must reproduce the above copyright
  30.      notice, this list of conditions and the following disclaimer in the
  31.      documentation and/or other materials provided with the
  32.      distribution.
  33.  
  34. Neither the name of the University nor the names of its contributors
  35. may be used to endorse or promote products derived from this software
  36. without specific prior written permission.
  37.  
  38. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  39. IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  40. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. PURPOSE.-->
  42. <meta http-equiv="Content-Style-Type" content="text/css">
  43. <style type="text/css"><!--
  44.   pre.display { font-family:inherit }
  45.   pre.format  { font-family:inherit }
  46.   pre.smalldisplay { font-family:inherit; font-size:smaller }
  47.   pre.smallformat  { font-family:inherit; font-size:smaller }
  48.   pre.smallexample { font-size:smaller }
  49.   pre.smalllisp    { font-size:smaller }
  50.   span.sc    { font-variant:small-caps }
  51.   span.roman { font-family:serif; font-weight:normal; } 
  52.   span.sansserif { font-family:sans-serif; font-weight:normal; } 
  53. --></style>
  54. </head>
  55. <body>
  56. <h1 class="settitle">flex: a fast lexical analyzer generator</h1>
  57.    <div class="contents">
  58. <h2>Table of Contents</h2>
  59. <ul>
  60. <li><a name="toc_Top" href="#Top">flex</a>
  61. <li><a name="toc_Copyright" href="#Copyright">1 Copyright</a>
  62. <li><a name="toc_Reporting-Bugs" href="#Reporting-Bugs">2 Reporting Bugs</a>
  63. <li><a name="toc_Introduction" href="#Introduction">3 Introduction</a>
  64. <li><a name="toc_Simple-Examples" href="#Simple-Examples">4 Some Simple Examples</a>
  65. <li><a name="toc_Format" href="#Format">5 Format of the Input File</a>
  66. <ul>
  67. <li><a href="#Definitions-Section">5.1 Format of the Definitions Section</a>
  68. <li><a href="#Rules-Section">5.2 Format of the Rules Section</a>
  69. <li><a href="#User-Code-Section">5.3 Format of the User Code Section</a>
  70. <li><a href="#Comments-in-the-Input">5.4 Comments in the Input</a>
  71. </li></ul>
  72. <li><a name="toc_Patterns" href="#Patterns">6 Patterns</a>
  73. <li><a name="toc_Matching" href="#Matching">7 How the Input Is Matched</a>
  74. <li><a name="toc_Actions" href="#Actions">8 Actions</a>
  75. <li><a name="toc_Generated-Scanner" href="#Generated-Scanner">9 The Generated Scanner</a>
  76. <li><a name="toc_Start-Conditions" href="#Start-Conditions">10 Start Conditions</a>
  77. <li><a name="toc_Multiple-Input-Buffers" href="#Multiple-Input-Buffers">11 Multiple Input Buffers</a>
  78. <li><a name="toc_EOF" href="#EOF">12 End-of-File Rules</a>
  79. <li><a name="toc_Misc-Macros" href="#Misc-Macros">13 Miscellaneous Macros</a>
  80. <li><a name="toc_User-Values" href="#User-Values">14 Values Available To the User</a>
  81. <li><a name="toc_Yacc" href="#Yacc">15 Interfacing with Yacc</a>
  82. <li><a name="toc_Scanner-Options" href="#Scanner-Options">16 Scanner Options</a>
  83. <ul>
  84. <li><a href="#Options-for-Specifing-Filenames">16.1 Options for Specifing Filenames</a>
  85. <li><a href="#Options-Affecting-Scanner-Behavior">16.2 Options Affecting Scanner Behavior</a>
  86. <li><a href="#Code_002dLevel-And-API-Options">16.3 Code-Level And API Options</a>
  87. <li><a href="#Options-for-Scanner-Speed-and-Size">16.4 Options for Scanner Speed and Size</a>
  88. <li><a href="#Debugging-Options">16.5 Debugging Options</a>
  89. <li><a href="#Miscellaneous-Options">16.6 Miscellaneous Options</a>
  90. </li></ul>
  91. <li><a name="toc_Performance" href="#Performance">17 Performance Considerations</a>
  92. <li><a name="toc_Cxx" href="#Cxx">18 Generating C++ Scanners</a>
  93. <li><a name="toc_Reentrant" href="#Reentrant">19 Reentrant C Scanners</a>
  94. <ul>
  95. <li><a href="#Reentrant-Uses">19.1 Uses for Reentrant Scanners</a>
  96. <li><a href="#Reentrant-Overview">19.2 An Overview of the Reentrant API</a>
  97. <li><a href="#Reentrant-Example">19.3 Reentrant Example</a>
  98. <li><a href="#Reentrant-Detail">19.4 The Reentrant API in Detail</a>
  99. <ul>
  100. <li><a href="#Specify-Reentrant">19.4.1 Declaring a Scanner As Reentrant</a>
  101. <li><a href="#Extra-Reentrant-Argument">19.4.2 The Extra Argument</a>
  102. <li><a href="#Global-Replacement">19.4.3 Global Variables Replaced By Macros</a>
  103. <li><a href="#Init-and-Destroy-Functions">19.4.4 Init and Destroy Functions</a>
  104. <li><a href="#Accessor-Methods">19.4.5 Accessing Variables with Reentrant Scanners</a>
  105. <li><a href="#Extra-Data">19.4.6 Extra Data</a>
  106. <li><a href="#About-yyscan_005ft">19.4.7 About yyscan_t</a>
  107. </li></ul>
  108. <li><a href="#Reentrant-Functions">19.5 Functions and Macros Available in Reentrant C Scanners</a>
  109. </li></ul>
  110. <li><a name="toc_Lex-and-Posix" href="#Lex-and-Posix">20 Incompatibilities with Lex and Posix</a>
  111. <li><a name="toc_Memory-Management" href="#Memory-Management">21 Memory Management</a>
  112. <ul>
  113. <li><a href="#The-Default-Memory-Management">21.1 The Default Memory Management</a>
  114. <li><a href="#Overriding-The-Default-Memory-Management">21.2 Overriding The Default Memory Management</a>
  115. <li><a href="#A-Note-About-yytext-And-Memory">21.3 A Note About yytext And Memory</a>
  116. </li></ul>
  117. <li><a name="toc_Serialized-Tables" href="#Serialized-Tables">22 Serialized Tables</a>
  118. <ul>
  119. <li><a href="#Creating-Serialized-Tables">22.1 Creating Serialized Tables</a>
  120. <li><a href="#Loading-and-Unloading-Serialized-Tables">22.2 Loading and Unloading Serialized Tables</a>
  121. <li><a href="#Tables-File-Format">22.3 Tables File Format</a>
  122. </li></ul>
  123. <li><a name="toc_Diagnostics" href="#Diagnostics">23 Diagnostics</a>
  124. <li><a name="toc_Limitations" href="#Limitations">24 Limitations</a>
  125. <li><a name="toc_Bibliography" href="#Bibliography">25 Additional Reading</a>
  126. <li><a name="toc_FAQ" href="#FAQ">FAQ</a>
  127. <ul>
  128. <li><a href="#When-was-flex-born_003f">When was flex born?</a>
  129. <li><a href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>
  130. <li><a href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>
  131. <li><a href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>
  132. <li><a href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>
  133. <li><a href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>
  134. <li><a href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>
  135. <li><a href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>
  136. <li><a href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>
  137. <li><a href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>
  138. <li><a href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>
  139. <li><a href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why can't I use fast or full tables with interactive mode?</a>
  140. <li><a href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>
  141. <li><a href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar can't I just parse it with flex?</a>
  142. <li><a href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesn't yyrestart() set the start state back to INITIAL?</a>
  143. <li><a href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>
  144. <li><a href="#The-period-isnt-working-the-way-I-expected_002e">The '.' isn't working the way I expected.</a>
  145. <li><a href="#Can-I-get-the-flex-manual-in-another-format_003f">Can I get the flex manual in another format?</a>
  146. <li><a href="#Does-there-exist-a-_0022faster_0022-NDFA_002d_003eDFA-algorithm_003f">Does there exist a "faster" NDFA->DFA algorithm?</a>
  147. <li><a href="#How-does-flex-compile-the-DFA-so-quickly_003f">How does flex compile the DFA so quickly?</a>
  148. <li><a href="#How-can-I-use-more-than-8192-rules_003f">How can I use more than 8192 rules?</a>
  149. <li><a href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>
  150. <li><a href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>
  151. <li><a href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>
  152. <li><a href="#Where-else-can-I-find-help_003f">Where else can I find help?</a>
  153. <li><a href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>
  154. <li><a href="#I-get-an-error-about-undefined-yywrap_0028_0029_002e">I get an error about undefined yywrap().</a>
  155. <li><a href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>
  156. <li><a href="#How-can-I-expand-macros-in-the-input_003f">How can I expand macros in the input?</a>
  157. <li><a href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>
  158. <li><a href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>
  159. <li><a href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>
  160. <li><a href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>
  161. <li><a href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>
  162. <li><a href="#Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f">Why doesn't flex have non-greedy operators like perl does?</a>
  163. <li><a href="#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e">Memory leak - 16386 bytes allocated by malloc.</a>
  164. <li><a href="#How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f">How do I track the byte offset for lseek()?</a>
  165. <li><a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>
  166. <li><a href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>
  167. <li><a href="#deleteme00">deleteme00</a>
  168. <li><a href="#Are-certain-equivalent-patterns-faster-than-others_003f">Are certain equivalent patterns faster than others?</a>
  169. <li><a href="#Is-backing-up-a-big-deal_003f">Is backing up a big deal?</a>
  170. <li><a href="#Can-I-fake-multi_002dbyte-character-support_003f">Can I fake multi-byte character support?</a>
  171. <li><a href="#deleteme01">deleteme01</a>
  172. <li><a href="#Can-you-discuss-some-flex-internals_003f">Can you discuss some flex internals?</a>
  173. <li><a href="#unput_0028_0029-messes-up-yy_005fat_005fbol">unput() messes up yy_at_bol</a>
  174. <li><a href="#The-_007c-operator-is-not-doing-what-I-want">The | operator is not doing what I want</a>
  175. <li><a href="#Why-can_0027t-flex-understand-this-variable-trailing-context-pattern_003f">Why can't flex understand this variable trailing context pattern?</a>
  176. <li><a href="#The-_005e-operator-isn_0027t-working">The ^ operator isn't working</a>
  177. <li><a href="#Trailing-context-is-getting-confused-with-trailing-optional-patterns">Trailing context is getting confused with trailing optional patterns</a>
  178. <li><a href="#Is-flex-GNU-or-not_003f">Is flex GNU or not?</a>
  179. <li><a href="#ERASEME53">ERASEME53</a>
  180. <li><a href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>
  181. <li><a href="#ERASEME55">ERASEME55</a>
  182. <li><a href="#ERASEME56">ERASEME56</a>
  183. <li><a href="#ERASEME57">ERASEME57</a>
  184. <li><a href="#Is-there-a-repository-for-flex-scanners_003f">Is there a repository for flex scanners?</a>
  185. <li><a href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>
  186. <li><a href="#Where-can-I-find-grammars-for-lex-and-yacc_003f">Where can I find grammars for lex and yacc?</a>
  187. <li><a href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>
  188. <li><a href="#unnamed_002dfaq_002d62">unnamed-faq-62</a>
  189. <li><a href="#unnamed_002dfaq_002d63">unnamed-faq-63</a>
  190. <li><a href="#unnamed_002dfaq_002d64">unnamed-faq-64</a>
  191. <li><a href="#unnamed_002dfaq_002d65">unnamed-faq-65</a>
  192. <li><a href="#unnamed_002dfaq_002d66">unnamed-faq-66</a>
  193. <li><a href="#unnamed_002dfaq_002d67">unnamed-faq-67</a>
  194. <li><a href="#unnamed_002dfaq_002d68">unnamed-faq-68</a>
  195. <li><a href="#unnamed_002dfaq_002d69">unnamed-faq-69</a>
  196. <li><a href="#unnamed_002dfaq_002d70">unnamed-faq-70</a>
  197. <li><a href="#unnamed_002dfaq_002d71">unnamed-faq-71</a>
  198. <li><a href="#unnamed_002dfaq_002d72">unnamed-faq-72</a>
  199. <li><a href="#unnamed_002dfaq_002d73">unnamed-faq-73</a>
  200. <li><a href="#unnamed_002dfaq_002d74">unnamed-faq-74</a>
  201. <li><a href="#unnamed_002dfaq_002d75">unnamed-faq-75</a>
  202. <li><a href="#unnamed_002dfaq_002d76">unnamed-faq-76</a>
  203. <li><a href="#unnamed_002dfaq_002d77">unnamed-faq-77</a>
  204. <li><a href="#unnamed_002dfaq_002d78">unnamed-faq-78</a>
  205. <li><a href="#unnamed_002dfaq_002d79">unnamed-faq-79</a>
  206. <li><a href="#unnamed_002dfaq_002d80">unnamed-faq-80</a>
  207. <li><a href="#unnamed_002dfaq_002d81">unnamed-faq-81</a>
  208. <li><a href="#unnamed_002dfaq_002d82">unnamed-faq-82</a>
  209. <li><a href="#unnamed_002dfaq_002d83">unnamed-faq-83</a>
  210. <li><a href="#unnamed_002dfaq_002d84">unnamed-faq-84</a>
  211. <li><a href="#unnamed_002dfaq_002d85">unnamed-faq-85</a>
  212. <li><a href="#unnamed_002dfaq_002d86">unnamed-faq-86</a>
  213. <li><a href="#unnamed_002dfaq_002d87">unnamed-faq-87</a>
  214. <li><a href="#unnamed_002dfaq_002d88">unnamed-faq-88</a>
  215. <li><a href="#unnamed_002dfaq_002d90">unnamed-faq-90</a>
  216. <li><a href="#unnamed_002dfaq_002d91">unnamed-faq-91</a>
  217. <li><a href="#unnamed_002dfaq_002d92">unnamed-faq-92</a>
  218. <li><a href="#unnamed_002dfaq_002d93">unnamed-faq-93</a>
  219. <li><a href="#unnamed_002dfaq_002d94">unnamed-faq-94</a>
  220. <li><a href="#unnamed_002dfaq_002d95">unnamed-faq-95</a>
  221. <li><a href="#unnamed_002dfaq_002d96">unnamed-faq-96</a>
  222. <li><a href="#unnamed_002dfaq_002d97">unnamed-faq-97</a>
  223. <li><a href="#unnamed_002dfaq_002d98">unnamed-faq-98</a>
  224. <li><a href="#unnamed_002dfaq_002d99">unnamed-faq-99</a>
  225. <li><a href="#unnamed_002dfaq_002d100">unnamed-faq-100</a>
  226. <li><a href="#unnamed_002dfaq_002d101">unnamed-faq-101</a>
  227. </li></ul>
  228. <li><a name="toc_Appendices" href="#Appendices">Appendix A Appendices</a>
  229. <ul>
  230. <li><a href="#Makefiles-and-Flex">A.1 Makefiles and Flex</a>
  231. <li><a href="#Bison-Bridge">A.2 C Scanners with Bison Parsers</a>
  232. <li><a href="#M4-Dependency">A.3 M4 Dependency</a>
  233. </li></ul>
  234. <li><a name="toc_Indices" href="#Indices">Indices</a>
  235. <ul>
  236. <li><a href="#Concept-Index">Concept Index</a>
  237. <li><a href="#Index-of-Functions-and-Macros">Index of Functions and Macros</a>
  238. <li><a href="#Index-of-Variables">Index of Variables</a>
  239. <li><a href="#Index-of-Data-Types">Index of Data Types</a>
  240. <li><a href="#Index-of-Hooks">Index of Hooks</a>
  241. <li><a href="#Index-of-Scanner-Options">Index of Scanner Options</a>
  242. </li></ul>
  243. </li></ul>
  244. </div>
  245.  
  246.  
  247.  
  248. <div class="node">
  249. <p><hr>
  250. <a name="Top"></a>
  251. Next: <a rel="next" accesskey="n" href="#Copyright">Copyright</a>,
  252. Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
  253. Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
  254.  
  255. </div>
  256.  
  257. <h2 class="unnumbered">flex</h2>
  258.  
  259. <p>This manual describes <code>flex</code>, a tool for generating programs that
  260. perform pattern-matching on text.  The manual includes both tutorial and
  261. reference sections.
  262.  
  263.    <p>This edition of <cite>The flex Manual</cite> documents <code>flex</code> version
  264. 2.5.31. It was last updated on 27 March 2003.
  265.  
  266. <ul class="menu">
  267. <li><a accesskey="1" href="#Copyright">Copyright</a>
  268. <li><a accesskey="2" href="#Reporting-Bugs">Reporting Bugs</a>
  269. <li><a accesskey="3" href="#Introduction">Introduction</a>
  270. <li><a accesskey="4" href="#Simple-Examples">Simple Examples</a>
  271. <li><a accesskey="5" href="#Format">Format</a>
  272. <li><a accesskey="6" href="#Patterns">Patterns</a>
  273. <li><a accesskey="7" href="#Matching">Matching</a>
  274. <li><a accesskey="8" href="#Actions">Actions</a>
  275. <li><a accesskey="9" href="#Generated-Scanner">Generated Scanner</a>
  276. <li><a href="#Start-Conditions">Start Conditions</a>
  277. <li><a href="#Multiple-Input-Buffers">Multiple Input Buffers</a>
  278. <li><a href="#EOF">EOF</a>
  279. <li><a href="#Misc-Macros">Misc Macros</a>
  280. <li><a href="#User-Values">User Values</a>
  281. <li><a href="#Yacc">Yacc</a>
  282. <li><a href="#Scanner-Options">Scanner Options</a>
  283. <li><a href="#Performance">Performance</a>
  284. <li><a href="#Cxx">Cxx</a>
  285. <li><a href="#Reentrant">Reentrant</a>
  286. <li><a href="#Lex-and-Posix">Lex and Posix</a>
  287. <li><a href="#Memory-Management">Memory Management</a>
  288. <li><a href="#Serialized-Tables">Serialized Tables</a>
  289. <li><a href="#Diagnostics">Diagnostics</a>
  290. <li><a href="#Limitations">Limitations</a>
  291. <li><a href="#Bibliography">Bibliography</a>
  292. <li><a href="#FAQ">FAQ</a>
  293. <li><a href="#Appendices">Appendices</a>
  294. <li><a href="#Indices">Indices</a>
  295.  
  296. </li></ul>
  297. <p>--- The Detailed Node Listing ---
  298.  
  299. <p>Format of the Input File
  300.  
  301. </p>
  302. <ul class="menu">
  303. <li><a href="#Definitions-Section">Definitions Section</a>
  304. <li><a href="#Rules-Section">Rules Section</a>
  305. <li><a href="#User-Code-Section">User Code Section</a>
  306. <li><a href="#Comments-in-the-Input">Comments in the Input</a>
  307.  
  308. </li></ul>
  309. <p>Scanner Options
  310.  
  311. </p>
  312. <ul class="menu">
  313. <li><a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a>
  314. <li><a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a>
  315. <li><a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a>
  316. <li><a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a>
  317. <li><a href="#Debugging-Options">Debugging Options</a>
  318. <li><a href="#Miscellaneous-Options">Miscellaneous Options</a>
  319.  
  320. </li></ul>
  321. <p>Reentrant C Scanners
  322.  
  323. </p>
  324. <ul class="menu">
  325. <li><a href="#Reentrant-Uses">Reentrant Uses</a>
  326. <li><a href="#Reentrant-Overview">Reentrant Overview</a>
  327. <li><a href="#Reentrant-Example">Reentrant Example</a>
  328. <li><a href="#Reentrant-Detail">Reentrant Detail</a>
  329. <li><a href="#Reentrant-Functions">Reentrant Functions</a>
  330.  
  331. </li></ul>
  332. <p>The Reentrant API in Detail
  333.  
  334. </p>
  335. <ul class="menu">
  336. <li><a href="#Specify-Reentrant">Specify Reentrant</a>
  337. <li><a href="#Extra-Reentrant-Argument">Extra Reentrant Argument</a>
  338. <li><a href="#Global-Replacement">Global Replacement</a>
  339. <li><a href="#Init-and-Destroy-Functions">Init and Destroy Functions</a>
  340. <li><a href="#Accessor-Methods">Accessor Methods</a>
  341. <li><a href="#Extra-Data">Extra Data</a>
  342. <li><a href="#About-yyscan_005ft">About yyscan_t</a>
  343.  
  344. </li></ul>
  345. <p>Memory Management
  346.  
  347. </p>
  348. <ul class="menu">
  349. <li><a href="#The-Default-Memory-Management">The Default Memory Management</a>
  350. <li><a href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a>
  351. <li><a href="#A-Note-About-yytext-And-Memory">A Note About yytext And Memory</a>
  352.  
  353. </li></ul>
  354. <p>Serialized Tables
  355.  
  356. </p>
  357. <ul class="menu">
  358. <li><a href="#Creating-Serialized-Tables">Creating Serialized Tables</a>
  359. <li><a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a>
  360. <li><a href="#Tables-File-Format">Tables File Format</a>
  361.  
  362. </li></ul>
  363. <p>FAQ
  364.  
  365. </p>
  366. <ul class="menu">
  367. <li><a href="#When-was-flex-born_003f">When was flex born?</a>
  368. <li><a href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>
  369. <li><a href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>
  370. <li><a href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>
  371. <li><a href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>
  372. <li><a href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>
  373. <li><a href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>
  374. <li><a href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>
  375. <li><a href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>
  376. <li><a href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>
  377. <li><a href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>
  378. <li><a href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why cant I use fast or full tables with interactive mode?</a>
  379. <li><a href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>
  380. <li><a href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar cant I just parse it with flex?</a>
  381. <li><a href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesnt yyrestart() set the start state back to INITIAL?</a>
  382. <li><a href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>
  383. <li><a href="#The-period-isnt-working-the-way-I-expected_002e">The period isnt working the way I expected.</a>
  384. <li><a href="#Can-I-get-the-flex-manual-in-another-format_003f">Can I get the flex manual in another format?</a>
  385. <li><a href="#Does-there-exist-a-_0022faster_0022-NDFA_002d_003eDFA-algorithm_003f">Does there exist a "faster" NDFA->DFA algorithm?</a>
  386. <li><a href="#How-does-flex-compile-the-DFA-so-quickly_003f">How does flex compile the DFA so quickly?</a>
  387. <li><a href="#How-can-I-use-more-than-8192-rules_003f">How can I use more than 8192 rules?</a>
  388. <li><a href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>
  389. <li><a href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>
  390. <li><a href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>
  391. <li><a href="#Where-else-can-I-find-help_003f">Where else can I find help?</a>
  392. <li><a href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>
  393. <li><a href="#I-get-an-error-about-undefined-yywrap_0028_0029_002e">I get an error about undefined yywrap().</a>
  394. <li><a href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>
  395. <li><a href="#How-can-I-expand-macros-in-the-input_003f">How can I expand macros in the input?</a>
  396. <li><a href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>
  397. <li><a href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>
  398. <li><a href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>
  399. <li><a href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>
  400. <li><a href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>
  401. <li><a href="#Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f">Why doesnt flex have non-greedy operators like perl does?</a>
  402. <li><a href="#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e">Memory leak - 16386 bytes allocated by malloc.</a>
  403. <li><a href="#How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f">How do I track the byte offset for lseek()?</a>
  404. <li><a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>
  405. <li><a href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>
  406. <li><a href="#deleteme00">deleteme00</a>
  407. <li><a href="#Are-certain-equivalent-patterns-faster-than-others_003f">Are certain equivalent patterns faster than others?</a>
  408. <li><a href="#Is-backing-up-a-big-deal_003f">Is backing up a big deal?</a>
  409. <li><a href="#Can-I-fake-multi_002dbyte-character-support_003f">Can I fake multi-byte character support?</a>
  410. <li><a href="#deleteme01">deleteme01</a>
  411. <li><a href="#Can-you-discuss-some-flex-internals_003f">Can you discuss some flex internals?</a>
  412. <li><a href="#unput_0028_0029-messes-up-yy_005fat_005fbol">unput() messes up yy_at_bol</a>
  413. <li><a href="#The-_007c-operator-is-not-doing-what-I-want">The | operator is not doing what I want</a>
  414. <li><a href="#Why-can_0027t-flex-understand-this-variable-trailing-context-pattern_003f">Why can't flex understand this variable trailing context pattern?</a>
  415. <li><a href="#The-_005e-operator-isn_0027t-working">The ^ operator isn't working</a>
  416. <li><a href="#Trailing-context-is-getting-confused-with-trailing-optional-patterns">Trailing context is getting confused with trailing optional patterns</a>
  417. <li><a href="#Is-flex-GNU-or-not_003f">Is flex GNU or not?</a>
  418. <li><a href="#ERASEME53">ERASEME53</a>
  419. <li><a href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>
  420. <li><a href="#ERASEME55">ERASEME55</a>
  421. <li><a href="#ERASEME56">ERASEME56</a>
  422. <li><a href="#ERASEME57">ERASEME57</a>
  423. <li><a href="#Is-there-a-repository-for-flex-scanners_003f">Is there a repository for flex scanners?</a>
  424. <li><a href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>
  425. <li><a href="#Where-can-I-find-grammars-for-lex-and-yacc_003f">Where can I find grammars for lex and yacc?</a>
  426. <li><a href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>
  427. <li><a href="#unnamed_002dfaq_002d62">unnamed-faq-62</a>
  428. <li><a href="#unnamed_002dfaq_002d63">unnamed-faq-63</a>
  429. <li><a href="#unnamed_002dfaq_002d64">unnamed-faq-64</a>
  430. <li><a href="#unnamed_002dfaq_002d65">unnamed-faq-65</a>
  431. <li><a href="#unnamed_002dfaq_002d66">unnamed-faq-66</a>
  432. <li><a href="#unnamed_002dfaq_002d67">unnamed-faq-67</a>
  433. <li><a href="#unnamed_002dfaq_002d68">unnamed-faq-68</a>
  434. <li><a href="#unnamed_002dfaq_002d69">unnamed-faq-69</a>
  435. <li><a href="#unnamed_002dfaq_002d70">unnamed-faq-70</a>
  436. <li><a href="#unnamed_002dfaq_002d71">unnamed-faq-71</a>
  437. <li><a href="#unnamed_002dfaq_002d72">unnamed-faq-72</a>
  438. <li><a href="#unnamed_002dfaq_002d73">unnamed-faq-73</a>
  439. <li><a href="#unnamed_002dfaq_002d74">unnamed-faq-74</a>
  440. <li><a href="#unnamed_002dfaq_002d75">unnamed-faq-75</a>
  441. <li><a href="#unnamed_002dfaq_002d76">unnamed-faq-76</a>
  442. <li><a href="#unnamed_002dfaq_002d77">unnamed-faq-77</a>
  443. <li><a href="#unnamed_002dfaq_002d78">unnamed-faq-78</a>
  444. <li><a href="#unnamed_002dfaq_002d79">unnamed-faq-79</a>
  445. <li><a href="#unnamed_002dfaq_002d80">unnamed-faq-80</a>
  446. <li><a href="#unnamed_002dfaq_002d81">unnamed-faq-81</a>
  447. <li><a href="#unnamed_002dfaq_002d82">unnamed-faq-82</a>
  448. <li><a href="#unnamed_002dfaq_002d83">unnamed-faq-83</a>
  449. <li><a href="#unnamed_002dfaq_002d84">unnamed-faq-84</a>
  450. <li><a href="#unnamed_002dfaq_002d85">unnamed-faq-85</a>
  451. <li><a href="#unnamed_002dfaq_002d86">unnamed-faq-86</a>
  452. <li><a href="#unnamed_002dfaq_002d87">unnamed-faq-87</a>
  453. <li><a href="#unnamed_002dfaq_002d88">unnamed-faq-88</a>
  454. <li><a href="#unnamed_002dfaq_002d90">unnamed-faq-90</a>
  455. <li><a href="#unnamed_002dfaq_002d91">unnamed-faq-91</a>
  456. <li><a href="#unnamed_002dfaq_002d92">unnamed-faq-92</a>
  457. <li><a href="#unnamed_002dfaq_002d93">unnamed-faq-93</a>
  458. <li><a href="#unnamed_002dfaq_002d94">unnamed-faq-94</a>
  459. <li><a href="#unnamed_002dfaq_002d95">unnamed-faq-95</a>
  460. <li><a href="#unnamed_002dfaq_002d96">unnamed-faq-96</a>
  461. <li><a href="#unnamed_002dfaq_002d97">unnamed-faq-97</a>
  462. <li><a href="#unnamed_002dfaq_002d98">unnamed-faq-98</a>
  463. <li><a href="#unnamed_002dfaq_002d99">unnamed-faq-99</a>
  464. <li><a href="#unnamed_002dfaq_002d100">unnamed-faq-100</a>
  465. <li><a href="#unnamed_002dfaq_002d101">unnamed-faq-101</a>
  466.  
  467. </li></ul>
  468. <p>Appendices
  469.  
  470. </p>
  471. <ul class="menu">
  472. <li><a href="#Makefiles-and-Flex">Makefiles and Flex</a>
  473. <li><a href="#Bison-Bridge">Bison Bridge</a>
  474. <li><a href="#M4-Dependency">M4 Dependency</a>
  475.  
  476. </li></ul>
  477. <p>Indices
  478.  
  479. </p>
  480. <ul class="menu">
  481. <li><a href="#Concept-Index">Concept Index</a>
  482. <li><a href="#Index-of-Functions-and-Macros">Index of Functions and Macros</a>
  483. <li><a href="#Index-of-Variables">Index of Variables</a>
  484. <li><a href="#Index-of-Data-Types">Index of Data Types</a>
  485. <li><a href="#Index-of-Hooks">Index of Hooks</a>
  486. <li><a href="#Index-of-Scanner-Options">Index of Scanner Options</a>
  487.  
  488.    </ul>
  489.  
  490. <div class="node">
  491. <p><hr>
  492. <a name="Copyright"></a>
  493. Next: <a rel="next" accesskey="n" href="#Reporting-Bugs">Reporting Bugs</a>,
  494. Previous: <a rel="previous" accesskey="p" href="#Top">Top</a>,
  495. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  496.  
  497. </div>
  498.  
  499. <h2 class="chapter">1 Copyright</h2>
  500.  
  501. <p><a name="index-copyright-of-flex-1"></a><a name="index-distributing-flex-2"></a>The flex manual is placed under the same licensing conditions as the
  502. rest of flex:
  503.  
  504.    <p>Copyright © 1990, 1997 The Regents of the University of California. 
  505. All rights reserved.
  506.  
  507.    <p>This code is derived from software contributed to Berkeley by
  508. Vern Paxson.
  509.  
  510.    <p>The United States Government has rights in this work pursuant
  511. to contract no. DE-AC03-76SF00098 between the United States
  512. Department of Energy and the University of California.
  513.  
  514.    <p>Redistribution and use in source and binary forms, with or without
  515. modification, are permitted provided that the following conditions
  516. are met:
  517.  
  518.      <ol type=1 start=1>
  519. <li> Redistributions of source code must retain the above copyright
  520. notice, this list of conditions and the following disclaimer.
  521.  
  522.      <li>Redistributions in binary form must reproduce the above copyright
  523. notice, this list of conditions and the following disclaimer in the
  524. documentation and/or other materials provided with the distribution.
  525.         </ol>
  526.  
  527.    <p>Neither the name of the University nor the names of its contributors
  528. may be used to endorse or promote products derived from this software
  529. without specific prior written permission.
  530.  
  531.    <p>THIS SOFTWARE IS PROVIDED “AS IS” AND WITHOUT ANY EXPRESS OR
  532. IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  533. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  534. PURPOSE.
  535.  
  536. <div class="node">
  537. <p><hr>
  538. <a name="Reporting-Bugs"></a>
  539. Next: <a rel="next" accesskey="n" href="#Introduction">Introduction</a>,
  540. Previous: <a rel="previous" accesskey="p" href="#Copyright">Copyright</a>,
  541. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  542.  
  543. </div>
  544.  
  545. <h2 class="chapter">2 Reporting Bugs</h2>
  546.  
  547. <p><a name="index-bugs_002c-reporting-3"></a><a name="index-reporting-bugs-4"></a>
  548. If you have problems with <code>flex</code> or think you have found a bug,
  549. please send mail detailing your problem to
  550. <a href="mailto:lex-help@lists.sourceforge.net">lex-help@lists.sourceforge.net</a>. Patches are always welcome.
  551.  
  552. <div class="node">
  553. <p><hr>
  554. <a name="Introduction"></a>
  555. Next: <a rel="next" accesskey="n" href="#Simple-Examples">Simple Examples</a>,
  556. Previous: <a rel="previous" accesskey="p" href="#Reporting-Bugs">Reporting Bugs</a>,
  557. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  558.  
  559. </div>
  560.  
  561. <h2 class="chapter">3 Introduction</h2>
  562.  
  563. <p><a name="index-scanner_002c-definition-of-5"></a><code>flex</code> is a tool for generating <dfn>scanners</dfn>.  A scanner is a
  564. program which recognizes lexical patterns in text.  The <code>flex</code>
  565. program reads the given input files, or its standard input if no file
  566. names are given, for a description of a scanner to generate.  The
  567. description is in the form of pairs of regular expressions and C code,
  568. called <dfn>rules</dfn>. <code>flex</code> generates as output a C source file,
  569. <samp><span class="file">lex.yy.c</span></samp> by default, which defines a routine <code>yylex()</code>. 
  570. This file can be compiled and linked with the flex runtime library to
  571. produce an executable.  When the executable is run, it analyzes its
  572. input for occurrences of the regular expressions.  Whenever it finds
  573. one, it executes the corresponding C code.
  574.  
  575. <div class="node">
  576. <p><hr>
  577. <a name="Simple-Examples"></a>
  578. Next: <a rel="next" accesskey="n" href="#Format">Format</a>,
  579. Previous: <a rel="previous" accesskey="p" href="#Introduction">Introduction</a>,
  580. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  581.  
  582. </div>
  583.  
  584. <h2 class="chapter">4 Some Simple Examples</h2>
  585.  
  586. <p>First some simple examples to get the flavor of how one uses
  587. <code>flex</code>.
  588.  
  589.    <p><a name="index-username-expansion-6"></a>The following <code>flex</code> input specifies a scanner which, when it
  590. encounters the string `<samp><span class="samp">username</span></samp>' will replace it with the user's
  591. login name:
  592.  
  593. <pre class="example"><pre class="verbatim">     
  594.          %%
  595.          username    printf( "%s", getlogin() );
  596. </pre>
  597. </pre>
  598.    <p><a name="index-default-rule-7"></a><a name="index-rules_002c-default-8"></a>By default, any text not matched by a <code>flex</code> scanner is copied to
  599. the output, so the net effect of this scanner is to copy its input file
  600. to its output with each occurrence of `<samp><span class="samp">username</span></samp>' expanded.  In this
  601. input, there is just one rule.  `<samp><span class="samp">username</span></samp>' is the <dfn>pattern</dfn> and
  602. the `<samp><span class="samp">printf</span></samp>' is the <dfn>action</dfn>.  The `<samp><span class="samp">%%</span></samp>' symbol marks the
  603. beginning of the rules.
  604.  
  605.    <p>Here's another simple example:
  606.  
  607.    <p><a name="index-counting-characters-and-lines-9"></a>
  608. <pre class="example"><pre class="verbatim">     
  609.                  int num_lines = 0, num_chars = 0;
  610.      
  611.          %%
  612.          \n      ++num_lines; ++num_chars;
  613.          .       ++num_chars;
  614.      
  615.          %%
  616.          main()
  617.                  {
  618.                  yylex();
  619.                  printf( "# of lines = %d, # of chars = %d\n",
  620.                          num_lines, num_chars );
  621.                  }
  622. </pre>
  623. </pre>
  624.    <p>This scanner counts the number of characters and the number of lines in
  625. its input. It produces no output other than the final report on the
  626. character and line counts.  The first line declares two globals,
  627. <code>num_lines</code> and <code>num_chars</code>, which are accessible both inside
  628. <code>yylex()</code> and in the <code>main()</code> routine declared after the
  629. second `<samp><span class="samp">%%</span></samp>'.  There are two rules, one which matches a newline
  630. (`<samp><span class="samp">\n</span></samp>') and increments both the line count and the character count,
  631. and one which matches any character other than a newline (indicated by
  632. the `<samp><span class="samp">.</span></samp>' regular expression).
  633.  
  634.    <p>A somewhat more complicated example:
  635.  
  636.    <p><a name="index-Pascal_002dlike-language-10"></a>
  637. <pre class="example"><pre class="verbatim">     
  638.          /* scanner for a toy Pascal-like language */
  639.      
  640.          %{
  641.          /* need this for the call to atof() below */
  642.          #include math.h>
  643.          %}
  644.      
  645.          DIGIT    [0-9]
  646.          ID       [a-z][a-z0-9]*
  647.      
  648.          %%
  649.      
  650.          {DIGIT}+    {
  651.                      printf( "An integer: %s (%d)\n", yytext,
  652.                              atoi( yytext ) );
  653.                      }
  654.      
  655.          {DIGIT}+"."{DIGIT}*        {
  656.                      printf( "A float: %s (%g)\n", yytext,
  657.                              atof( yytext ) );
  658.                      }
  659.      
  660.          if|then|begin|end|procedure|function        {
  661.                      printf( "A keyword: %s\n", yytext );
  662.                      }
  663.      
  664.          {ID}        printf( "An identifier: %s\n", yytext );
  665.      
  666.          "+"|"-"|"*"|"/"   printf( "An operator: %s\n", yytext );
  667.      
  668.          "{"[\^{}}\n]*"}"     /* eat up one-line comments */
  669.      
  670.          [ \t\n]+          /* eat up whitespace */
  671.      
  672.          .           printf( "Unrecognized character: %s\n", yytext );
  673.      
  674.          %%
  675.      
  676.          main( argc, argv )
  677.          int argc;
  678.          char **argv;
  679.              {
  680.              ++argv, --argc;  /* skip over program name */
  681.              if ( argc > 0 )
  682.                      yyin = fopen( argv[0], "r" );
  683.              else
  684.                      yyin = stdin;
  685.      
  686.              yylex();
  687.              }
  688. </pre>
  689. </pre>
  690.    <p>This is the beginnings of a simple scanner for a language like Pascal. 
  691. It identifies different types of <dfn>tokens</dfn> and reports on what it has
  692. seen.
  693.  
  694.    <p>The details of this example will be explained in the following
  695. sections.
  696.  
  697. <div class="node">
  698. <p><hr>
  699. <a name="Format"></a>
  700. Next: <a rel="next" accesskey="n" href="#Patterns">Patterns</a>,
  701. Previous: <a rel="previous" accesskey="p" href="#Simple-Examples">Simple Examples</a>,
  702. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  703.  
  704. </div>
  705.  
  706. <h2 class="chapter">5 Format of the Input File</h2>
  707.  
  708. <p><a name="index-format-of-flex-input-11"></a><a name="index-input_002c-format-of-12"></a><a name="index-file-format-13"></a><a name="index-sections-of-flex-input-14"></a>
  709. The <code>flex</code> input file consists of three sections, separated by a
  710. line containing only `<samp><span class="samp">%%</span></samp>'.
  711.  
  712.    <p><a name="index-format-of-input-file-15"></a>
  713. <pre class="example"><pre class="verbatim">     
  714.          definitions
  715.          %%
  716.          rules
  717.          %%
  718.          user code
  719. </pre>
  720. </pre>
  721.    <ul class="menu">
  722. <li><a accesskey="1" href="#Definitions-Section">Definitions Section</a>
  723. <li><a accesskey="2" href="#Rules-Section">Rules Section</a>
  724. <li><a accesskey="3" href="#User-Code-Section">User Code Section</a>
  725. <li><a accesskey="4" href="#Comments-in-the-Input">Comments in the Input</a>
  726. </ul>
  727.  
  728. <div class="node">
  729. <p><hr>
  730. <a name="Definitions-Section"></a>
  731. Next: <a rel="next" accesskey="n" href="#Rules-Section">Rules Section</a>,
  732. Previous: <a rel="previous" accesskey="p" href="#Format">Format</a>,
  733. Up: <a rel="up" accesskey="u" href="#Format">Format</a>
  734.  
  735. </div>
  736.  
  737. <h3 class="section">5.1 Format of the Definitions Section</h3>
  738.  
  739. <p><a name="index-input-file_002c-Definitions-section-16"></a><a name="index-Definitions_002c-in-flex-input-17"></a>The <dfn>definitions section</dfn> contains declarations of simple <dfn>name</dfn>
  740. definitions to simplify the scanner specification, and declarations of
  741. <dfn>start conditions</dfn>, which are explained in a later section.
  742.  
  743.    <p><a name="index-aliases_002c-how-to-define-18"></a><a name="index-pattern-aliases_002c-how-to-define-19"></a>Name definitions have the form:
  744.  
  745. <pre class="example"><pre class="verbatim">     
  746.          name definition
  747. </pre>
  748. </pre>
  749.    <p>The `<samp><span class="samp">name</span></samp>' is a word beginning with a letter or an underscore
  750. (`<samp><span class="samp">_</span></samp>') followed by zero or more letters, digits, `<samp><span class="samp">_</span></samp>', or
  751. `<samp><span class="samp">-</span></samp>' (dash).  The definition is taken to begin at the first
  752. non-whitespace character following the name and continuing to the end of
  753. the line.  The definition can subsequently be referred to using
  754. `<samp><span class="samp">{name}</span></samp>', which will expand to `<samp><span class="samp">(definition)</span></samp>'.  For example,
  755.  
  756.    <p><a name="index-pattern-aliases_002c-defining-20"></a><a name="index-defining-pattern-aliases-21"></a>
  757. <pre class="example"><pre class="verbatim">     
  758.          DIGIT    [0-9]
  759.          ID       [a-z][a-z0-9]*
  760. </pre>
  761. </pre>
  762.    <p>Defines `<samp><span class="samp">DIGIT</span></samp>' to be a regular expression which matches a single
  763. digit, and `<samp><span class="samp">ID</span></samp>' to be a regular expression which matches a letter
  764. followed by zero-or-more letters-or-digits.  A subsequent reference to
  765.  
  766.    <p><a name="index-pattern-aliases_002c-use-of-22"></a>
  767. <pre class="example"><pre class="verbatim">     
  768.          {DIGIT}+"."{DIGIT}*
  769. </pre>
  770. </pre>
  771.    <p>is identical to
  772.  
  773. <pre class="example"><pre class="verbatim">     
  774.          ([0-9])+"."([0-9])*
  775. </pre>
  776. </pre>
  777.    <p>and matches one-or-more digits followed by a `<samp><span class="samp">.</span></samp>' followed by
  778. zero-or-more digits.
  779.  
  780.    <p><a name="index-comments-in-flex-input-23"></a>An unindented comment (i.e., a line
  781. beginning with `<samp><span class="samp">/*</span></samp>') is copied verbatim to the output up
  782. to the next `<samp><span class="samp">*/</span></samp>'.
  783.  
  784.    <p><a name="index-g_t_0025_0040_007b-and-_0025_0040_007d_002c-in-Definitions-Section-24"></a><a name="index-embedding-C-code-in-flex-input-25"></a><a name="index-C-code-in-flex-input-26"></a>Any <em>indented</em> text or text enclosed in `<samp><span class="samp">%{</span></samp>' and `<samp><span class="samp">%}</span></samp>'
  785. is also copied verbatim to the output (with the %{ and %} symbols
  786. removed).  The %{ and %} symbols must appear unindented on lines by
  787. themselves.
  788.  
  789.    <p><a name="index-g_t_0025top-27"></a>
  790. A <code>%top</code> block is similar to a `<samp><span class="samp">%{</span></samp>' ... `<samp><span class="samp">%}</span></samp>' block, except
  791. that the code in a <code>%top</code> block is relocated to the <em>top</em> of the
  792. generated file, before any flex definitions <a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a>. 
  793. The <code>%top</code> block is useful when you want certain preprocessor macros to be
  794. defined or certain files to be included before the generated code. 
  795. The single characters, `<samp><span class="samp">{</span></samp>'  and `<samp><span class="samp">}</span></samp>' are used to delimit the
  796. <code>%top</code> block, as show in the example below:
  797.  
  798. <pre class="example"><pre class="verbatim">     
  799.          %top{
  800.              /* This code goes at the "top" of the generated file. */
  801.              #include <stdint.h>
  802.              #include <inttypes.h>
  803.          }
  804. </pre>
  805. </pre>
  806.    <p>Multiple <code>%top</code> blocks are allowed, and their order is preserved.
  807.  
  808. <div class="node">
  809. <p><hr>
  810. <a name="Rules-Section"></a>
  811. Next: <a rel="next" accesskey="n" href="#User-Code-Section">User Code Section</a>,
  812. Previous: <a rel="previous" accesskey="p" href="#Definitions-Section">Definitions Section</a>,
  813. Up: <a rel="up" accesskey="u" href="#Format">Format</a>
  814.  
  815. </div>
  816.  
  817. <h3 class="section">5.2 Format of the Rules Section</h3>
  818.  
  819. <p><a name="index-input-file_002c-Rules-Section-28"></a><a name="index-rules_002c-in-flex-input-29"></a>The <dfn>rules</dfn> section of the <code>flex</code> input contains a series of
  820. rules of the form:
  821.  
  822. <pre class="example"><pre class="verbatim">     
  823.          pattern   action
  824. </pre>
  825. </pre>
  826.    <p>where the pattern must be unindented and the action must begin
  827. on the same line. 
  828. See <a href="#Patterns">Patterns</a>, for a further description of patterns and actions.
  829.  
  830.    <p>In the rules section, any indented or %{ %} enclosed text appearing
  831. before the first rule may be used to declare variables which are local
  832. to the scanning routine and (after the declarations) code which is to be
  833. executed whenever the scanning routine is entered.  Other indented or
  834. %{ %} text in the rule section is still copied to the output, but its
  835. meaning is not well-defined and it may well cause compile-time errors
  836. (this feature is present for <acronym>POSIX</acronym> compliance. See <a href="#Lex-and-Posix">Lex and Posix</a>, for other such features).
  837.  
  838.    <p>Any <em>indented</em> text or text enclosed in `<samp><span class="samp">%{</span></samp>' and `<samp><span class="samp">%}</span></samp>'
  839. is copied verbatim to the output (with the %{ and %} symbols removed). 
  840. The %{ and %} symbols must appear unindented on lines by themselves.
  841.  
  842. <div class="node">
  843. <p><hr>
  844. <a name="User-Code-Section"></a>
  845. Next: <a rel="next" accesskey="n" href="#Comments-in-the-Input">Comments in the Input</a>,
  846. Previous: <a rel="previous" accesskey="p" href="#Rules-Section">Rules Section</a>,
  847. Up: <a rel="up" accesskey="u" href="#Format">Format</a>
  848.  
  849. </div>
  850.  
  851. <h3 class="section">5.3 Format of the User Code Section</h3>
  852.  
  853. <p><a name="index-input-file_002c-user-code-Section-30"></a><a name="index-user-code_002c-in-flex-input-31"></a>The user code section is simply copied to <samp><span class="file">lex.yy.c</span></samp> verbatim.  It
  854. is used for companion routines which call or are called by the scanner. 
  855. The presence of this section is optional; if it is missing, the second
  856. `<samp><span class="samp">%%</span></samp>' in the input file may be skipped, too.
  857.  
  858. <div class="node">
  859. <p><hr>
  860. <a name="Comments-in-the-Input"></a>
  861. Previous: <a rel="previous" accesskey="p" href="#User-Code-Section">User Code Section</a>,
  862. Up: <a rel="up" accesskey="u" href="#Format">Format</a>
  863.  
  864. </div>
  865.  
  866. <h3 class="section">5.4 Comments in the Input</h3>
  867.  
  868. <p><a name="index-comments_002c-syntax-of-32"></a>Flex supports C-style comments, that is, anything between /* and */ is
  869. considered a comment. Whenever flex encounters a comment, it copies the
  870. entire comment verbatim to the generated source code. Comments may
  871. appear just about anywhere, but with the following exceptions:
  872.  
  873.      
  874. <a name="index-comments_002c-in-rules-section-33"></a>
  875. <ul><li>Comments may not appear in the Rules Section wherever flex is expecting
  876. a regular expression. This means comments may not appear at the
  877. beginning of a line, or immediately following a list of scanner states. 
  878. <li>Comments may not appear on an `<samp><span class="samp">%option</span></samp>' line in the Definitions
  879. Section. 
  880. </ul>
  881.  
  882.    <p>If you want to follow a simple rule, then always begin a comment on a
  883. new line, with one or more whitespace characters before the initial
  884. `<samp><span class="samp">/*</span></samp>').  This rule will work anywhere in the input file.
  885.  
  886.    <p>All the comments in the following example are valid:
  887.  
  888.    <p><a name="index-comments_002c-valid-uses-of-34"></a><a name="index-comments-in-the-input-35"></a>
  889. <pre class="example"><pre class="verbatim">     
  890.      %{
  891.      /* code block */
  892.      %}
  893.      
  894.      /* Definitions Section */
  895.      %x STATE_X
  896.      
  897.      %%
  898.          /* Rules Section */
  899.      ruleA   /* after regex */ { /* code block */ } /* after code block */
  900.              /* Rules Section (indented) */
  901.      <STATE_X>{
  902.      ruleC   ECHO;
  903.      ruleD   ECHO;
  904.      %{
  905.      /* code block */
  906.      %}
  907.      }
  908.      %%
  909.      /* User Code Section */
  910.      
  911. </pre>
  912. </pre>
  913.    <div class="node">
  914. <p><hr>
  915. <a name="Patterns"></a>
  916. Next: <a rel="next" accesskey="n" href="#Matching">Matching</a>,
  917. Previous: <a rel="previous" accesskey="p" href="#Format">Format</a>,
  918. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  919.  
  920. </div>
  921.  
  922. <h2 class="chapter">6 Patterns</h2>
  923.  
  924. <p><a name="index-patterns_002c-in-rules-section-36"></a><a name="index-regular-expressions_002c-in-patterns-37"></a>The patterns in the input (see <a href="#Rules-Section">Rules Section</a>) are written using an
  925. extended set of regular expressions.  These are:
  926.  
  927.    <p><a name="index-patterns_002c-syntax-38"></a><a name="index-patterns_002c-syntax-39"></a>
  928.      <dl>
  929. <dt>`<samp><span class="samp">x</span></samp>'<dd>match the character 'x'
  930.  
  931.      <br><dt>`<samp><span class="samp">.</span></samp>'<dd>any character (byte) except newline
  932.  
  933.      <p><a name="index-g_t_005b_005d-in-patterns-40"></a><a name="index-character-classes-in-patterns_002c-syntax-of-41"></a><a name="index-POSIX_002c-character-classes-in-patterns_002c-syntax-of-42"></a><br><dt>`<samp><span class="samp">[xyz]</span></samp>'<dd>a <dfn>character class</dfn>; in this case, the pattern
  934. matches either an 'x', a 'y', or a 'z'
  935.  
  936.      <p><a name="index-ranges-in-patterns-43"></a><br><dt>`<samp><span class="samp">[abj-oZ]</span></samp>'<dd>a "character class" with a range in it; matches
  937. an 'a', a 'b', any letter from 'j' through 'o',
  938. or a 'Z'
  939.  
  940.      <p><a name="index-ranges-in-patterns_002c-negating-44"></a><a name="index-negating-ranges-in-patterns-45"></a><br><dt>`<samp><span class="samp">[^A-Z]</span></samp>'<dd>a "negated character class", i.e., any character
  941. but those in the class.  In this case, any
  942. character EXCEPT an uppercase letter.
  943.  
  944.      <br><dt>`<samp><span class="samp">[^A-Z\n]</span></samp>'<dd>any character EXCEPT an uppercase letter or
  945. a newline
  946.  
  947.      <br><dt>`<samp><span class="samp">r*</span></samp>'<dd>zero or more r's, where r is any regular expression
  948.  
  949.      <br><dt>`<samp><span class="samp">r+</span></samp>'<dd>one or more r's
  950.  
  951.      <br><dt>`<samp><span class="samp">r?</span></samp>'<dd>zero or one r's (that is, “an optional r”)
  952.  
  953.      <p><a name="index-braces-in-patterns-46"></a><br><dt>`<samp><span class="samp">r{2,5}</span></samp>'<dd>anywhere from two to five r's
  954.  
  955.      <br><dt>`<samp><span class="samp">r{2,}</span></samp>'<dd>two or more r's
  956.  
  957.      <br><dt>`<samp><span class="samp">r{4}</span></samp>'<dd>exactly 4 r's
  958.  
  959.      <p><a name="index-pattern-aliases_002c-expansion-of-47"></a><br><dt>`<samp><span class="samp">{name}</span></samp>'<dd>the expansion of the `<samp><span class="samp">name</span></samp>' definition
  960. (see <a href="#Format">Format</a>).
  961.  
  962.      <p><a name="index-literal-text-in-patterns_002c-syntax-of-48"></a><a name="index-verbatim-text-in-patterns_002c-syntax-of-49"></a><br><dt>`<samp><span class="samp">"[xyz]\"foo"</span></samp>'<dd>the literal string: `<samp><span class="samp">[xyz]"foo</span></samp>'
  963.  
  964.      <p><a name="index-escape-sequences-in-patterns_002c-syntax-of-50"></a><br><dt>`<samp><span class="samp">\X</span></samp>'<dd>if X is `<samp><span class="samp">a</span></samp>', `<samp><span class="samp">b</span></samp>', `<samp><span class="samp">f</span></samp>', `<samp><span class="samp">n</span></samp>', `<samp><span class="samp">r</span></samp>', `<samp><span class="samp">t</span></samp>', or
  965. `<samp><span class="samp">v</span></samp>', then the ANSI-C interpretation of `<samp><span class="samp">\x</span></samp>'.  Otherwise, a
  966. literal `<samp><span class="samp">X</span></samp>' (used to escape operators such as `<samp><span class="samp">*</span></samp>')
  967.  
  968.      <p><a name="index-NULL-character-in-patterns_002c-syntax-of-51"></a><br><dt>`<samp><span class="samp">\0</span></samp>'<dd>a NUL character (ASCII code 0)
  969.  
  970.      <p><a name="index-octal-characters-in-patterns-52"></a><br><dt>`<samp><span class="samp">\123</span></samp>'<dd>the character with octal value 123
  971.  
  972.      <br><dt>`<samp><span class="samp">\x2a</span></samp>'<dd>the character with hexadecimal value 2a
  973.  
  974.      <br><dt>`<samp><span class="samp">(r)</span></samp>'<dd>match an `<samp><span class="samp">r</span></samp>'; parentheses are used to override precedence (see below)
  975.  
  976.      <p><a name="index-concatenation_002c-in-patterns-53"></a><br><dt>`<samp><span class="samp">rs</span></samp>'<dd>the regular expression `<samp><span class="samp">r</span></samp>' followed by the regular expression `<samp><span class="samp">s</span></samp>'; called
  977. <dfn>concatenation</dfn>
  978.  
  979.      <br><dt>`<samp><span class="samp">r|s</span></samp>'<dd>either an `<samp><span class="samp">r</span></samp>' or an `<samp><span class="samp">s</span></samp>'
  980.  
  981.      <p><a name="index-trailing-context_002c-in-patterns-54"></a><br><dt>`<samp><span class="samp">r/s</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>' but only if it is followed by an `<samp><span class="samp">s</span></samp>'.  The text matched by `<samp><span class="samp">s</span></samp>' is
  982. included when determining whether this rule is the longest match, but is
  983. then returned to the input before the action is executed.  So the action
  984. only sees the text matched by `<samp><span class="samp">r</span></samp>'.  This type of pattern is called
  985. <dfn>trailing context</dfn>.  (There are some combinations of `<samp><span class="samp">r/s</span></samp>' that flex
  986. cannot match correctly. See <a href="#Limitations">Limitations</a>, regarding dangerous trailing
  987. context.)
  988.  
  989.      <p><a name="index-beginning-of-line_002c-in-patterns-55"></a><a name="index-BOL_002c-in-patterns-56"></a><br><dt>`<samp><span class="samp">^r</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>', but only at the beginning of a line (i.e.,
  990. when just starting to scan, or right after a
  991. newline has been scanned).
  992.  
  993.      <p><a name="index-end-of-line_002c-in-patterns-57"></a><a name="index-EOL_002c-in-patterns-58"></a><br><dt>`<samp><span class="samp">r$</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>', but only at the end of a line (i.e., just before a
  994. newline).  Equivalent to `<samp><span class="samp">r/\n</span></samp>'.
  995.  
  996.      <p><a name="index-newline_002c-matching-in-patterns-59"></a>Note that <code>flex</code>'s notion of “newline” is exactly
  997. whatever the C compiler used to compile <code>flex</code>
  998. interprets `<samp><span class="samp">\n</span></samp>' as; in particular, on some DOS
  999. systems you must either filter out `<samp><span class="samp">\r</span></samp>'s in the
  1000. input yourself, or explicitly use `<samp><span class="samp">r/\r\n</span></samp>' for `<samp><span class="samp">r$</span></samp>'.
  1001.  
  1002.      <p><a name="index-start-conditions_002c-in-patterns-60"></a><br><dt>`<samp><span class="samp"><s>r</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>', but only in start condition <code>s</code> (see <a href="#Start-Conditions">Start Conditions</a> for discussion of start conditions).
  1003.  
  1004.      <br><dt>`<samp><span class="samp"><s1,s2,s3>r</span></samp>'<dd>same, but in any of start conditions <code>s1</code>, <code>s2</code>, or <code>s3</code>.
  1005.  
  1006.      <br><dt>`<samp><span class="samp"><*>r</span></samp>'<dd>an `<samp><span class="samp">r</span></samp>' in any start condition, even an exclusive one.
  1007.  
  1008.      <p><a name="index-end-of-file_002c-in-patterns-61"></a><a name="index-EOF-in-patterns_002c-syntax-of-62"></a><br><dt>`<samp><span class="samp"><<EOF>></span></samp>'<dd>an end-of-file.
  1009.  
  1010.      <br><dt>`<samp><span class="samp"><s1,s2><<EOF>></span></samp>'<dd>an end-of-file when in start condition <code>s1</code> or <code>s2</code>
  1011. </dl>
  1012.  
  1013.    <p>Note that inside of a character class, all regular expression operators
  1014. lose their special meaning except escape (`<samp><span class="samp">\</span></samp>') and the character class
  1015. operators, `<samp><span class="samp">-</span></samp>', `<samp><span class="samp">]]</span></samp>', and, at the beginning of the class, `<samp><span class="samp">^</span></samp>'.
  1016.  
  1017.    <p><a name="index-patterns_002c-precedence-of-operators-63"></a>The regular expressions listed above are grouped according to
  1018. precedence, from highest precedence at the top to lowest at the bottom. 
  1019. Those grouped together have equal precedence (see special note on the
  1020. precedence of the repeat operator, `<samp><span class="samp">{}</span></samp>', under the documentation
  1021. for the `<samp><span class="samp">--posix</span></samp>' POSIX compliance option).  For example,
  1022.  
  1023.    <p><a name="index-patterns_002c-grouping-and-precedence-64"></a>
  1024. <pre class="example"><pre class="verbatim">     
  1025.          foo|bar*
  1026. </pre>
  1027. </pre>
  1028.    <p>is the same as
  1029.  
  1030. <pre class="example"><pre class="verbatim">     
  1031.          (foo)|(ba(r*))
  1032. </pre>
  1033. </pre>
  1034.    <p>since the `<samp><span class="samp">*</span></samp>' operator has higher precedence than concatenation,
  1035. and concatenation higher than alternation (`<samp><span class="samp">|</span></samp>').  This pattern
  1036. therefore matches <em>either</em> the string `<samp><span class="samp">foo</span></samp>' <em>or</em> the
  1037. string `<samp><span class="samp">ba</span></samp>' followed by zero-or-more `<samp><span class="samp">r</span></samp>''s.  To match
  1038. `<samp><span class="samp">foo</span></samp>' or zero-or-more repetitions of the string `<samp><span class="samp">bar</span></samp>', use:
  1039.  
  1040. <pre class="example"><pre class="verbatim">     
  1041.          foo|(bar)*
  1042. </pre>
  1043. </pre>
  1044.    <p>And to match a sequence of zero or more repetitions of `<samp><span class="samp">foo</span></samp>' and
  1045. `<samp><span class="samp">bar</span></samp>':
  1046.  
  1047.    <p><a name="index-patterns_002c-repetitions-with-grouping-65"></a>
  1048. <pre class="example"><pre class="verbatim">     
  1049.          (foo|bar)*
  1050. </pre>
  1051. </pre>
  1052.    <p><a name="index-character-classes-in-patterns-66"></a>In addition to characters and ranges of characters, character classes
  1053. can also contain <dfn>character class expressions</dfn>.  These are
  1054. expressions enclosed inside `<samp><span class="samp">[</span></samp>': and `<samp><span class="samp">:]</span></samp>' delimiters (which
  1055. themselves must appear between the `<samp><span class="samp">[</span></samp>' and `<samp><span class="samp">]</span></samp>' of the
  1056. character class. Other elements may occur inside the character class,
  1057. too).  The valid expressions are:
  1058.  
  1059.    <p><a name="index-patterns_002c-valid-character-classes-67"></a>
  1060. <pre class="example"><pre class="verbatim">     
  1061.          [:alnum:] [:alpha:] [:blank:]
  1062.          [:cntrl:] [:digit:] [:graph:]
  1063.          [:lower:] [:print:] [:punct:]
  1064.          [:space:] [:upper:] [:xdigit:]
  1065. </pre>
  1066. </pre>
  1067.    <p>These expressions all designate a set of characters equivalent to the
  1068. corresponding standard C <code>isXXX</code> function.  For example,
  1069. `<samp><span class="samp">[:alnum:]</span></samp>' designates those characters for which <code>isalnum()</code>
  1070. returns true - i.e., any alphabetic or numeric character.  Some systems
  1071. don't provide <code>isblank()</code>, so flex defines `<samp><span class="samp">[:blank:]</span></samp>' as a
  1072. blank or a tab.
  1073.  
  1074.    <p>For example, the following character classes are all equivalent:
  1075.  
  1076.    <p><a name="index-character-classes_002c-equivalence-of-68"></a><a name="index-patterns_002c-character-class-equivalence-69"></a>
  1077. <pre class="example"><pre class="verbatim">     
  1078.          [[:alnum:]]
  1079.          [[:alpha:][:digit:]]
  1080.          [[:alpha:][0-9]]
  1081.          [a-zA-Z0-9]
  1082. </pre>
  1083. </pre>
  1084.    <p>Some notes on patterns are in order.
  1085.  
  1086.      
  1087. <a name="index-case_002dinsensitive_002c-effect-on-character-classes-70"></a>
  1088. <ul><li>If your scanner is case-insensitive (the `<samp><span class="samp">-i</span></samp>' flag), then
  1089. `<samp><span class="samp">[:upper:]</span></samp>' and `<samp><span class="samp">[:lower:]</span></samp>' are equivalent to
  1090. `<samp><span class="samp">[:alpha:]</span></samp>'.
  1091.  
  1092.      <p><a name="case-and-character-ranges"></a>
  1093. <li>Character classes with ranges, such as `<samp><span class="samp">[a-Z]</span></samp>', should be used with
  1094. caution in a case-insensitive scanner if the range spans upper or lowercase
  1095. characters. Flex does not know if you want to fold all upper and lowercase
  1096. characters together, or if you want the literal numeric range specified (with
  1097. no case folding). When in doubt, flex will assume that you meant the literal
  1098. numeric range, and will issue a warning. The exception to this rule is a
  1099. character range such as `<samp><span class="samp">[a-z]</span></samp>' or `<samp><span class="samp">[S-W]</span></samp>' where it is obvious that you
  1100. want case-folding to occur. Here are some examples with the `<samp><span class="samp">-i</span></samp>' flag
  1101. enabled:
  1102.  
  1103.      <p><table summary=""><tr align="left"><td valign="top">Range </td><td valign="top">Result </td><td valign="top">Literal Range </td><td valign="top">Alternate Range
  1104. <br></td></tr><tr align="left"><td valign="top">`<samp><span class="samp">[a-t]</span></samp>' </td><td valign="top">ok </td><td valign="top">`<samp><span class="samp">[a-tA-T]</span></samp>' </td><td valign="top">
  1105. <br></td></tr><tr align="left"><td valign="top">`<samp><span class="samp">[A-T]</span></samp>' </td><td valign="top">ok </td><td valign="top">`<samp><span class="samp">[a-tA-T]</span></samp>' </td><td valign="top">
  1106. <br></td></tr><tr align="left"><td valign="top">`<samp><span class="samp">[A-t]</span></samp>' </td><td valign="top">ambiguous </td><td valign="top">`<samp><span class="samp">[A-Z\[\\\]_`a-t]</span></samp>' </td><td valign="top">`<samp><span class="samp">[a-tA-T]</span></samp>'
  1107. <br></td></tr><tr align="left"><td valign="top">`<samp><span class="samp">[_-{]</span></samp>' </td><td valign="top">ambiguous </td><td valign="top">`<samp><span class="samp">[_`a-z{]</span></samp>' </td><td valign="top">`<samp><span class="samp">[_`a-zA-Z{]</span></samp>'
  1108. <br></td></tr><tr align="left"><td valign="top">`<samp><span class="samp">[@-C]</span></samp>' </td><td valign="top">ambiguous </td><td valign="top">`<samp><span class="samp">[@ABC]</span></samp>' </td><td valign="top">`<samp><span class="samp">[@A-Z\[\\\]_`abc]</span></samp>'
  1109.      <br></td></tr></table>
  1110.  
  1111.      <p><a name="index-end-of-line_002c-in-negated-character-classes-71"></a><a name="index-EOL_002c-in-negated-character-classes-72"></a><li>A negated character class such as the example `<samp><span class="samp">[^A-Z]</span></samp>' above
  1112. <em>will</em> match a newline unless `<samp><span class="samp">\n</span></samp>' (or an equivalent escape
  1113. sequence) is one of the characters explicitly present in the negated
  1114. character class (e.g., `<samp><span class="samp">[^A-Z\n]</span></samp>').  This is unlike how many other
  1115. regular expression tools treat negated character classes, but
  1116. unfortunately the inconsistency is historically entrenched.  Matching
  1117. newlines means that a pattern like `<samp><span class="samp">[^"]*</span></samp>' can match the entire
  1118. input unless there's another quote in the input.
  1119.  
  1120.      <p><a name="index-trailing-context_002c-limits-of-73"></a><a name="index-g_t_005e-as-non_002dspecial-character-in-patterns-74"></a><a name="index-g_t_0024-as-normal-character-in-patterns-75"></a><li>A rule can have at most one instance of trailing context (the `<samp><span class="samp">/</span></samp>' operator
  1121. or the `<samp><span class="samp">$</span></samp>' operator).  The start condition, `<samp><span class="samp">^</span></samp>', and `<samp><span class="samp"><<EOF>></span></samp>' patterns
  1122. can only occur at the beginning of a pattern, and, as well as with `<samp><span class="samp">/</span></samp>' and `<samp><span class="samp">$</span></samp>',
  1123. cannot be grouped inside parentheses.  A `<samp><span class="samp">^</span></samp>' which does not occur at
  1124. the beginning of a rule or a `<samp><span class="samp">$</span></samp>' which does not occur at the end of
  1125. a rule loses its special properties and is treated as a normal character.
  1126.  
  1127.      <li>The following are invalid:
  1128.  
  1129.      <p><a name="index-patterns_002c-invalid-trailing-context-76"></a>
  1130.      <pre class="example">     <pre class="verbatim">          
  1131.               foo/bar$
  1132.               <sc1>foo<sc2>bar
  1133.      </pre>
  1134.      </pre>
  1135.      <p>Note that the first of these can be written `<samp><span class="samp">foo/bar\n</span></samp>'.
  1136.  
  1137.      <li>The following will result in `<samp><span class="samp">$</span></samp>' or `<samp><span class="samp">^</span></samp>' being treated as a normal character:
  1138.  
  1139.      <p><a name="index-patterns_002c-special-characters-treated-as-non_002dspecial-77"></a>
  1140.      <pre class="example">     <pre class="verbatim">          
  1141.               foo|(bar$)
  1142.               foo|^bar
  1143.      </pre>
  1144.      </pre>
  1145.      <p>If the desired meaning is a `<samp><span class="samp">foo</span></samp>' or a
  1146. `<samp><span class="samp">bar</span></samp>'-followed-by-a-newline, the following could be used (the
  1147. special <code>|</code> action is explained below, see <a href="#Actions">Actions</a>):
  1148.  
  1149.      <p><a name="index-patterns_002c-end-of-line-78"></a>
  1150.      <pre class="example">     <pre class="verbatim">          
  1151.               foo      |
  1152.               bar$     /* action goes here */
  1153.      </pre>
  1154.      </pre>
  1155.      <p>A similar trick will work for matching a `<samp><span class="samp">foo</span></samp>' or a
  1156. `<samp><span class="samp">bar</span></samp>'-at-the-beginning-of-a-line. 
  1157. </ul>
  1158.  
  1159. <div class="node">
  1160. <p><hr>
  1161. <a name="Matching"></a>
  1162. Next: <a rel="next" accesskey="n" href="#Actions">Actions</a>,
  1163. Previous: <a rel="previous" accesskey="p" href="#Patterns">Patterns</a>,
  1164. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  1165.  
  1166. </div>
  1167.  
  1168. <h2 class="chapter">7 How the Input Is Matched</h2>
  1169.  
  1170. <p><a name="index-patterns_002c-matching-79"></a><a name="index-input_002c-matching-80"></a><a name="index-trailing-context_002c-matching-81"></a><a name="index-matching_002c-and-trailing-context-82"></a><a name="index-matching_002c-length-of-83"></a><a name="index-matching_002c-multiple-matches-84"></a>When the generated scanner is run, it analyzes its input looking for
  1171. strings which match any of its patterns.  If it finds more than one
  1172. match, it takes the one matching the most text (for trailing context
  1173. rules, this includes the length of the trailing part, even though it
  1174. will then be returned to the input).  If it finds two or more matches of
  1175. the same length, the rule listed first in the <code>flex</code> input file is
  1176. chosen.
  1177.  
  1178.    <p><a name="index-token-85"></a><a name="index-yytext-86"></a><a name="index-yyleng-87"></a>Once the match is determined, the text corresponding to the match
  1179. (called the <dfn>token</dfn>) is made available in the global character
  1180. pointer <code>yytext</code>, and its length in the global integer
  1181. <code>yyleng</code>.  The <dfn>action</dfn> corresponding to the matched pattern is
  1182. then executed (see <a href="#Actions">Actions</a>), and then the remaining input is scanned
  1183. for another match.
  1184.  
  1185.    <p><a name="index-default-rule-88"></a>If no match is found, then the <dfn>default rule</dfn> is executed: the next
  1186. character in the input is considered matched and copied to the standard
  1187. output.  Thus, the simplest valid <code>flex</code> input is:
  1188.  
  1189.    <p><a name="index-minimal-scanner-89"></a>
  1190. <pre class="example"><pre class="verbatim">     
  1191.          %%
  1192. </pre>
  1193. </pre>
  1194.    <p>which generates a scanner that simply copies its input (one character at
  1195. a time) to its output.
  1196.  
  1197.    <p><a name="index-yytext_002c-two-types-of-90"></a><a name="index-g_t_0025array_002c-use-of-91"></a><a name="index-g_t_0025pointer_002c-use-of-92"></a><a name="index-yytext-93"></a>Note that <code>yytext</code> can be defined in two different ways: either as
  1198. a character <em>pointer</em> or as a character <em>array</em>. You can
  1199. control which definition <code>flex</code> uses by including one of the
  1200. special directives <code>%pointer</code> or <code>%array</code> in the first
  1201. (definitions) section of your flex input.  The default is
  1202. <code>%pointer</code>, unless you use the `<samp><span class="samp">-l</span></samp>' lex compatibility option,
  1203. in which case <code>yytext</code> will be an array.  The advantage of using
  1204. <code>%pointer</code> is substantially faster scanning and no buffer overflow
  1205. when matching very large tokens (unless you run out of dynamic memory). 
  1206. The disadvantage is that you are restricted in how your actions can
  1207. modify <code>yytext</code> (see <a href="#Actions">Actions</a>), and calls to the <code>unput()</code>
  1208. function destroys the present contents of <code>yytext</code>, which can be a
  1209. considerable porting headache when moving between different <code>lex</code>
  1210. versions.
  1211.  
  1212.    <p><a name="index-g_t_0025array_002c-advantages-of-94"></a>The advantage of <code>%array</code> is that you can then modify <code>yytext</code>
  1213. to your heart's content, and calls to <code>unput()</code> do not destroy
  1214. <code>yytext</code> (see <a href="#Actions">Actions</a>).  Furthermore, existing <code>lex</code>
  1215. programs sometimes access <code>yytext</code> externally using declarations of
  1216. the form:
  1217.  
  1218. <pre class="example"><pre class="verbatim">     
  1219.          extern char yytext[];
  1220. </pre>
  1221. </pre>
  1222.    <p>This definition is erroneous when used with <code>%pointer</code>, but correct
  1223. for <code>%array</code>.
  1224.  
  1225.    <p>The <code>%array</code> declaration defines <code>yytext</code> to be an array of
  1226. <code>YYLMAX</code> characters, which defaults to a fairly large value.  You
  1227. can change the size by simply #define'ing <code>YYLMAX</code> to a different
  1228. value in the first section of your <code>flex</code> input.  As mentioned
  1229. above, with <code>%pointer</code> yytext grows dynamically to accommodate
  1230. large tokens.  While this means your <code>%pointer</code> scanner can
  1231. accommodate very large tokens (such as matching entire blocks of
  1232. comments), bear in mind that each time the scanner must resize
  1233. <code>yytext</code> it also must rescan the entire token from the beginning,
  1234. so matching such tokens can prove slow.  <code>yytext</code> presently does
  1235. <em>not</em> dynamically grow if a call to <code>unput()</code> results in too
  1236. much text being pushed back; instead, a run-time error results.
  1237.  
  1238.    <p><a name="index-g_t_0025array_002c-with-C_002b_002b-95"></a>Also note that you cannot use <code>%array</code> with C++ scanner classes
  1239. (see <a href="#Cxx">Cxx</a>).
  1240.  
  1241. <div class="node">
  1242. <p><hr>
  1243. <a name="Actions"></a>
  1244. Next: <a rel="next" accesskey="n" href="#Generated-Scanner">Generated Scanner</a>,
  1245. Previous: <a rel="previous" accesskey="p" href="#Matching">Matching</a>,
  1246. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  1247.  
  1248. </div>
  1249.  
  1250. <h2 class="chapter">8 Actions</h2>
  1251.  
  1252. <p><a name="index-actions-96"></a>Each pattern in a rule has a corresponding <dfn>action</dfn>, which can be
  1253. any arbitrary C statement.  The pattern ends at the first non-escaped
  1254. whitespace character; the remainder of the line is its action.  If the
  1255. action is empty, then when the pattern is matched the input token is
  1256. simply discarded.  For example, here is the specification for a program
  1257. which deletes all occurrences of `<samp><span class="samp">zap me</span></samp>' from its input:
  1258.  
  1259.    <p><a name="index-deleting-lines-from-input-97"></a>
  1260. <pre class="example"><pre class="verbatim">     
  1261.          %%
  1262.          "zap me"
  1263. </pre>
  1264. </pre>
  1265.    <p>This example will copy all other characters in the input to the output
  1266. since they will be matched by the default rule.
  1267.  
  1268.    <p>Here is a program which compresses multiple blanks and tabs down to a
  1269. single blank, and throws away whitespace found at the end of a line:
  1270.  
  1271.    <p><a name="index-whitespace_002c-compressing-98"></a><a name="index-compressing-whitespace-99"></a>
  1272. <pre class="example"><pre class="verbatim">     
  1273.          %%
  1274.          [ \t]+        putchar( ' ' );
  1275.          [ \t]+$       /* ignore this token */
  1276. </pre>
  1277. </pre>
  1278.    <p><a name="index-g_t_0025_0040_007b-and-_0025_0040_007d_002c-in-Rules-Section-100"></a><a name="index-actions_002c-use-of-_0040_007b-and-_0040_007d-101"></a><a name="index-actions_002c-embedded-C-strings-102"></a><a name="index-C_002dstrings_002c-in-actions-103"></a><a name="index-comments_002c-in-actions-104"></a>If the action contains a `<samp><span class="samp">}</span></samp>', then the action spans till the
  1279. balancing `<samp><span class="samp">}</span></samp>' is found, and the action may cross multiple lines. 
  1280. <code>flex</code> knows about C strings and comments and won't be fooled by
  1281. braces found within them, but also allows actions to begin with
  1282. `<samp><span class="samp">%{</span></samp>' and will consider the action to be all the text up to the
  1283. next `<samp><span class="samp">%}</span></samp>' (regardless of ordinary braces inside the action).
  1284.  
  1285.    <p><a name="index-g_t_007c_002c-in-actions-105"></a>An action consisting solely of a vertical bar (`<samp><span class="samp">|</span></samp>') means “same as the
  1286. action for the next rule”.  See below for an illustration.
  1287.  
  1288.    <p>Actions can include arbitrary C code, including <code>return</code> statements
  1289. to return a value to whatever routine called <code>yylex()</code>.  Each time
  1290. <code>yylex()</code> is called it continues processing tokens from where it
  1291. last left off until it either reaches the end of the file or executes a
  1292. return.
  1293.  
  1294.    <p><a name="index-yytext_002c-modification-of-106"></a>Actions are free to modify <code>yytext</code> except for lengthening it
  1295. (adding characters to its end–these will overwrite later characters in
  1296. the input stream).  This however does not apply when using <code>%array</code>
  1297. (see <a href="#Matching">Matching</a>). In that case, <code>yytext</code> may be freely modified
  1298. in any way.
  1299.  
  1300.    <p><a name="index-yyleng_002c-modification-of-107"></a><a name="index-yymore_002c-and-yyleng-108"></a>Actions are free to modify <code>yyleng</code> except they should not do so if
  1301. the action also includes use of <code>yymore()</code> (see below).
  1302.  
  1303.    <p><a name="index-preprocessor-macros_002c-for-use-in-actions-109"></a>There are a number of special directives which can be included within an
  1304. action:
  1305.  
  1306.      <dl>
  1307. <dt><code>ECHO</code><dd><a name="index-ECHO-110"></a>copies yytext to the scanner's output.
  1308.  
  1309.      <br><dt><code>BEGIN</code><dd><a name="index-BEGIN-111"></a>followed by the name of a start condition places the scanner in the
  1310. corresponding start condition (see below).
  1311.  
  1312.      <br><dt><code>REJECT</code><dd><a name="index-REJECT-112"></a>directs the scanner to proceed on to the “second best” rule which
  1313. matched the input (or a prefix of the input).  The rule is chosen as
  1314. described above in <a href="#Matching">Matching</a>, and <code>yytext</code> and <code>yyleng</code>
  1315. set up appropriately.  It may either be one which matched as much text
  1316. as the originally chosen rule but came later in the <code>flex</code> input
  1317. file, or one which matched less text.  For example, the following will
  1318. both count the words in the input and call the routine <code>special()</code>
  1319. whenever `<samp><span class="samp">frob</span></samp>' is seen:
  1320.  
  1321.      <pre class="example">     <pre class="verbatim">          
  1322.                       int word_count = 0;
  1323.               %%
  1324.           
  1325.               frob        special(); REJECT;
  1326.               [^ \t\n]+   ++word_count;
  1327.      </pre>
  1328.      </pre>
  1329.      <p>Without the <code>REJECT</code>, any occurences of `<samp><span class="samp">frob</span></samp>' in the input
  1330. would not be counted as words, since the scanner normally executes only
  1331. one action per token.  Multiple uses of <code>REJECT</code> are allowed, each
  1332. one finding the next best choice to the currently active rule.  For
  1333. example, when the following scanner scans the token `<samp><span class="samp">abcd</span></samp>', it will
  1334. write `<samp><span class="samp">abcdabcaba</span></samp>' to the output:
  1335.  
  1336.      <p><a name="index-REJECT_002c-calling-multiple-times-113"></a><a name="index-g_t_007c_002c-use-of-114"></a>
  1337.      <pre class="example">     <pre class="verbatim">          
  1338.               %%
  1339.               a        |
  1340.               ab       |
  1341.               abc      |
  1342.               abcd     ECHO; REJECT;
  1343.               .|\n     /* eat up any unmatched character */
  1344.      </pre>
  1345.      </pre>
  1346.      <p>The first three rules share the fourth's action since they use the
  1347. special `<samp><span class="samp">|</span></samp>' action.
  1348.  
  1349.      <p><code>REJECT</code> is a particularly expensive feature in terms of scanner
  1350. performance; if it is used in <em>any</em> of the scanner's actions it
  1351. will slow down <em>all</em> of the scanner's matching.  Furthermore,
  1352. <code>REJECT</code> cannot be used with the `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' options
  1353. (see <a href="#Scanner-Options">Scanner Options</a>).
  1354.  
  1355.      <p>Note also that unlike the other special actions, <code>REJECT</code> is a
  1356. <em>branch</em>.  code immediately following it in the action will
  1357. <em>not</em> be executed.
  1358.  
  1359.      <br><dt><code>yymore()</code><dd><a name="index-yymore_0028_0029-115"></a>tells the scanner that the next time it matches a rule, the
  1360. corresponding token should be <em>appended</em> onto the current value of
  1361. <code>yytext</code> rather than replacing it.  For example, given the input
  1362. `<samp><span class="samp">mega-kludge</span></samp>' the following will write `<samp><span class="samp">mega-mega-kludge</span></samp>' to
  1363. the output:
  1364.  
  1365.      <p><a name="index-yymore_0028_0029_002c-mega_002dkludge-116"></a><a name="index-yymore_0028_0029-to-append-token-to-previous-token-117"></a>
  1366.      <pre class="example">     <pre class="verbatim">          
  1367.               %%
  1368.               mega-    ECHO; yymore();
  1369.               kludge   ECHO;
  1370.      </pre>
  1371.      </pre>
  1372.      <p>First `<samp><span class="samp">mega-</span></samp>' is matched and echoed to the output.  Then `<samp><span class="samp">kludge</span></samp>'
  1373. is matched, but the previous `<samp><span class="samp">mega-</span></samp>' is still hanging around at the
  1374. beginning of
  1375. <code>yytext</code>
  1376. so the
  1377. <code>ECHO</code>
  1378. for the `<samp><span class="samp">kludge</span></samp>' rule will actually write `<samp><span class="samp">mega-kludge</span></samp>'. 
  1379. </dl>
  1380.  
  1381.    <p><a name="index-yymore_002c-performance-penalty-of-118"></a>Two notes regarding use of <code>yymore()</code>.  First, <code>yymore()</code>
  1382. depends on the value of <code>yyleng</code> correctly reflecting the size of
  1383. the current token, so you must not modify <code>yyleng</code> if you are using
  1384. <code>yymore()</code>.  Second, the presence of <code>yymore()</code> in the
  1385. scanner's action entails a minor performance penalty in the scanner's
  1386. matching speed.
  1387.  
  1388.    <p><a name="index-yyless_0028_0029-119"></a><code>yyless(n)</code> returns all but the first <code>n</code> characters of the
  1389. current token back to the input stream, where they will be rescanned
  1390. when the scanner looks for the next match.  <code>yytext</code> and
  1391. <code>yyleng</code> are adjusted appropriately (e.g., <code>yyleng</code> will now
  1392. be equal to <code>n</code>).  For example, on the input `<samp><span class="samp">foobar</span></samp>' the
  1393. following will write out `<samp><span class="samp">foobarbar</span></samp>':
  1394.  
  1395.    <p><a name="index-yyless_0028_0029_002c-pushing-back-characters-120"></a><a name="index-pushing-back-characters-with-yyless-121"></a>
  1396. <pre class="example"><pre class="verbatim">     
  1397.          %%
  1398.          foobar    ECHO; yyless(3);
  1399.          [a-z]+    ECHO;
  1400. </pre>
  1401. </pre>
  1402.    <p>An argument of 0 to <code>yyless()</code> will cause the entire current input
  1403. string to be scanned again.  Unless you've changed how the scanner will
  1404. subsequently process its input (using <code>BEGIN</code>, for example), this
  1405. will result in an endless loop.
  1406.  
  1407.    <p>Note that <code>yyless()</code> is a macro and can only be used in the flex
  1408. input file, not from other source files.
  1409.  
  1410.    <p><a name="index-unput_0028_0029-122"></a><a name="index-pushing-back-characters-with-unput-123"></a><code>unput(c)</code> puts the character <code>c</code> back onto the input stream. 
  1411. It will be the next character scanned.  The following action will take
  1412. the current token and cause it to be rescanned enclosed in parentheses.
  1413.  
  1414.    <p><a name="index-unput_0028_0029_002c-pushing-back-characters-124"></a><a name="index-pushing-back-characters-with-unput_0028_0029-125"></a>
  1415. <pre class="example"><pre class="verbatim">     
  1416.          {
  1417.          int i;
  1418.          /* Copy yytext because unput() trashes yytext */
  1419.          char *yycopy = strdup( yytext );
  1420.          unput( ')' );
  1421.          for ( i = yyleng - 1; i >= 0; --i )
  1422.              unput( yycopy[i] );
  1423.          unput( '(' );
  1424.          free( yycopy );
  1425.          }
  1426. </pre>
  1427. </pre>
  1428.    <p>Note that since each <code>unput()</code> puts the given character back at the
  1429. <em>beginning</em> of the input stream, pushing back strings must be done
  1430. back-to-front.
  1431.  
  1432.    <p><a name="index-g_t_0025pointer_002c-and-unput_0028_0029-126"></a><a name="index-unput_0028_0029_002c-and-_0025pointer-127"></a>An important potential problem when using <code>unput()</code> is that if you
  1433. are using <code>%pointer</code> (the default), a call to <code>unput()</code>
  1434. <em>destroys</em> the contents of <code>yytext</code>, starting with its
  1435. rightmost character and devouring one character to the left with each
  1436. call.  If you need the value of <code>yytext</code> preserved after a call to
  1437. <code>unput()</code> (as in the above example), you must either first copy it
  1438. elsewhere, or build your scanner using <code>%array</code> instead
  1439. (see <a href="#Matching">Matching</a>).
  1440.  
  1441.    <p><a name="index-pushing-back-EOF-128"></a><a name="index-EOF_002c-pushing-back-129"></a>Finally, note that you cannot put back `<samp><span class="samp">EOF</span></samp>' to attempt to mark the
  1442. input stream with an end-of-file.
  1443.  
  1444.    <p><a name="index-input_0028_0029-130"></a><code>input()</code> reads the next character from the input stream.  For
  1445. example, the following is one way to eat up C comments:
  1446.  
  1447.    <p><a name="index-comments_002c-discarding-131"></a><a name="index-discarding-C-comments-132"></a>
  1448. <pre class="example"><pre class="verbatim">     
  1449.          %%
  1450.          "/*"        {
  1451.                      register int c;
  1452.      
  1453.                      for ( ; ; )
  1454.                          {
  1455.                          while ( (c = input()) != '*' &&
  1456.                                  c != EOF )
  1457.                              ;    /* eat up text of comment */
  1458.      
  1459.                          if ( c == '*' )
  1460.                              {
  1461.                              while ( (c = input()) == '*' )
  1462.                                  ;
  1463.                              if ( c == '/' )
  1464.                                  break;    /* found the end */
  1465.                              }
  1466.      
  1467.                          if ( c == EOF )
  1468.                              {
  1469.                              error( "EOF in comment" );
  1470.                              break;
  1471.                              }
  1472.                          }
  1473.                      }
  1474. </pre>
  1475. </pre>
  1476.    <p><a name="index-input_0028_0029_002c-and-C_002b_002b-133"></a><a name="index-yyinput_0028_0029-134"></a>(Note that if the scanner is compiled using <code>C++</code>, then
  1477. <code>input()</code> is instead referred to as <b>yyinput()</b>, in order to
  1478. avoid a name clash with the <code>C++</code> stream by the name of
  1479. <code>input</code>.)
  1480.  
  1481.    <p><a name="index-flushing-the-internal-buffer-135"></a><a name="index-YY_005fFLUSH_005fBUFFER_0028_0029-136"></a><code>YY_FLUSH_BUFFER()</code> flushes the scanner's internal buffer so that
  1482. the next time the scanner attempts to match a token, it will first
  1483. refill the buffer using <code>YY_INPUT()</code> (see <a href="#Generated-Scanner">Generated Scanner</a>). 
  1484. This action is a special case of the more general
  1485. <code>yy_flush_buffer()</code> function, described below (see <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a>)
  1486.  
  1487.    <p><a name="index-yyterminate_0028_0029-137"></a><a name="index-terminating-with-yyterminate_0028_0029-138"></a><a name="index-exiting-with-yyterminate_0028_0029-139"></a><a name="index-halting-with-yyterminate_0028_0029-140"></a><code>yyterminate()</code> can be used in lieu of a return statement in an
  1488. action.  It terminates the scanner and returns a 0 to the scanner's
  1489. caller, indicating “all done”.  By default, <code>yyterminate()</code> is
  1490. also called when an end-of-file is encountered.  It is a macro and may
  1491. be redefined.
  1492.  
  1493. <div class="node">
  1494. <p><hr>
  1495. <a name="Generated-Scanner"></a>
  1496. Next: <a rel="next" accesskey="n" href="#Start-Conditions">Start Conditions</a>,
  1497. Previous: <a rel="previous" accesskey="p" href="#Actions">Actions</a>,
  1498. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  1499.  
  1500. </div>
  1501.  
  1502. <h2 class="chapter">9 The Generated Scanner</h2>
  1503.  
  1504. <p><a name="index-yylex_0028_0029_002c-in-generated-scanner-141"></a>The output of <code>flex</code> is the file <samp><span class="file">lex.yy.c</span></samp>, which contains
  1505. the scanning routine <code>yylex()</code>, a number of tables used by it for
  1506. matching tokens, and a number of auxiliary routines and macros.  By
  1507. default, <code>yylex()</code> is declared as follows:
  1508.  
  1509. <pre class="example"><pre class="verbatim">     
  1510.          int yylex()
  1511.              {
  1512.              ... various definitions and the actions in here ...
  1513.              }
  1514. </pre>
  1515. </pre>
  1516.    <p><a name="index-yylex_0028_0029_002c-overriding-142"></a>(If your environment supports function prototypes, then it will be
  1517. <code>int yylex( void )</code>.)  This definition may be changed by defining
  1518. the <code>YY_DECL</code> macro.  For example, you could use:
  1519.  
  1520.    <p><a name="index-yylex_002c-overriding-the-prototype-of-143"></a>
  1521. <pre class="example"><pre class="verbatim">     
  1522.          #define YY_DECL float lexscan( a, b ) float a, b;
  1523. </pre>
  1524. </pre>
  1525.    <p>to give the scanning routine the name <code>lexscan</code>, returning a float,
  1526. and taking two floats as arguments.  Note that if you give arguments to
  1527. the scanning routine using a K&R-style/non-prototyped function
  1528. declaration, you must terminate the definition with a semi-colon (;).
  1529.  
  1530.    <p><code>flex</code> generates `<samp><span class="samp">C99</span></samp>' function definitions by
  1531. default. However flex does have the ability to generate obsolete, er,
  1532. `<samp><span class="samp">traditional</span></samp>', function definitions. This is to support
  1533. bootstrapping gcc on old systems.  Unfortunately, traditional
  1534. definitions prevent us from using any standard data types smaller than
  1535. int (such as short, char, or bool) as function arguments.  For this
  1536. reason, future versions of <code>flex</code> may generate standard C99 code
  1537. only, leaving K&R-style functions to the historians.  Currently, if you
  1538. do <strong>not</strong> want `<samp><span class="samp">C99</span></samp>' definitions, then you must use
  1539. <code>%option noansi-definitions</code>.
  1540.  
  1541.    <p><a name="index-stdin_002c-default-for-yyin-144"></a><a name="index-yyin-145"></a>Whenever <code>yylex()</code> is called, it scans tokens from the global input
  1542. file <samp><span class="file">yyin</span></samp> (which defaults to stdin).  It continues until it
  1543. either reaches an end-of-file (at which point it returns the value 0) or
  1544. one of its actions executes a <code>return</code> statement.
  1545.  
  1546.    <p><a name="index-EOF-and-yyrestart_0028_0029-146"></a><a name="index-end_002dof_002dfile_002c-and-yyrestart_0028_0029-147"></a><a name="index-yyrestart_0028_0029-148"></a>If the scanner reaches an end-of-file, subsequent calls are undefined
  1547. unless either <samp><span class="file">yyin</span></samp> is pointed at a new input file (in which case
  1548. scanning continues from that file), or <code>yyrestart()</code> is called. 
  1549. <code>yyrestart()</code> takes one argument, a <code>FILE *</code> pointer (which
  1550. can be NULL, if you've set up <code>YY_INPUT</code> to scan from a source other
  1551. than <code>yyin</code>), and initializes <samp><span class="file">yyin</span></samp> for scanning from that
  1552. file.  Essentially there is no difference between just assigning
  1553. <samp><span class="file">yyin</span></samp> to a new input file or using <code>yyrestart()</code> to do so;
  1554. the latter is available for compatibility with previous versions of
  1555. <code>flex</code>, and because it can be used to switch input files in the
  1556. middle of scanning.  It can also be used to throw away the current input
  1557. buffer, by calling it with an argument of <samp><span class="file">yyin</span></samp>; but it would be
  1558. better to use <code>YY_FLUSH_BUFFER</code> (see <a href="#Actions">Actions</a>).  Note that
  1559. <code>yyrestart()</code> does <em>not</em> reset the start condition to
  1560. <code>INITIAL</code> (see <a href="#Start-Conditions">Start Conditions</a>).
  1561.  
  1562.    <p><a name="index-RETURN_002c-within-actions-149"></a>If <code>yylex()</code> stops scanning due to executing a <code>return</code>
  1563. statement in one of the actions, the scanner may then be called again
  1564. and it will resume scanning where it left off.
  1565.  
  1566.    <p><a name="index-YY_005fINPUT-150"></a>By default (and for purposes of efficiency), the scanner uses
  1567. block-reads rather than simple <code>getc()</code> calls to read characters
  1568. from <samp><span class="file">yyin</span></samp>.  The nature of how it gets its input can be controlled
  1569. by defining the <code>YY_INPUT</code> macro.  The calling sequence for
  1570. <code>YY_INPUT()</code> is <code>YY_INPUT(buf,result,max_size)</code>.  Its action
  1571. is to place up to <code>max_size</code> characters in the character array
  1572. <code>buf</code> and return in the integer variable <code>result</code> either the
  1573. number of characters read or the constant <code>YY_NULL</code> (0 on Unix
  1574. systems) to indicate `<samp><span class="samp">EOF</span></samp>'.  The default <code>YY_INPUT</code> reads from
  1575. the global file-pointer <samp><span class="file">yyin</span></samp>.
  1576.  
  1577.    <p><a name="index-YY_005fINPUT_002c-overriding-151"></a>Here is a sample definition of <code>YY_INPUT</code> (in the definitions
  1578. section of the input file):
  1579.  
  1580. <pre class="example"><pre class="verbatim">     
  1581.          %{
  1582.          #define YY_INPUT(buf,result,max_size) \
  1583.              { \
  1584.              int c = getchar(); \
  1585.              result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
  1586.              }
  1587.          %}
  1588. </pre>
  1589. </pre>
  1590.    <p>This definition will change the input processing to occur one character
  1591. at a time.
  1592.  
  1593.    <p><a name="index-yywrap_0028_0029-152"></a>When the scanner receives an end-of-file indication from YY_INPUT, it
  1594. then checks the <code>yywrap()</code> function.  If <code>yywrap()</code> returns
  1595. false (zero), then it is assumed that the function has gone ahead and
  1596. set up <samp><span class="file">yyin</span></samp> to point to another input file, and scanning
  1597. continues.  If it returns true (non-zero), then the scanner terminates,
  1598. returning 0 to its caller.  Note that in either case, the start
  1599. condition remains unchanged; it does <em>not</em> revert to
  1600. <code>INITIAL</code>.
  1601.  
  1602.    <p><a name="index-yywrap_002c-default-for-153"></a><a name="index-nowrap_002c-_0025option-154"></a><a name="index-g_t_0025option-nowrap-155"></a>If you do not supply your own version of <code>yywrap()</code>, then you must
  1603. either use <code>%option noyywrap</code> (in which case the scanner behaves as
  1604. though <code>yywrap()</code> returned 1), or you must link with `<samp><span class="samp">-lfl</span></samp>' to
  1605. obtain the default version of the routine, which always returns 1.
  1606.  
  1607.    <p>For scanning from in-memory buffers (e.g., scanning strings), see
  1608. <a href="#Scanning-Strings">Scanning Strings</a>. See <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a>.
  1609.  
  1610.    <p><a name="index-ECHO_002c-and-yyout-156"></a><a name="index-yyout-157"></a><a name="index-stdout_002c-as-default-for-yyout-158"></a>The scanner writes its <code>ECHO</code> output to the <samp><span class="file">yyout</span></samp> global
  1611. (default, <samp><span class="file">stdout</span></samp>), which may be redefined by the user simply by
  1612. assigning it to some other <code>FILE</code> pointer.
  1613.  
  1614. <div class="node">
  1615. <p><hr>
  1616. <a name="Start-Conditions"></a>
  1617. Next: <a rel="next" accesskey="n" href="#Multiple-Input-Buffers">Multiple Input Buffers</a>,
  1618. Previous: <a rel="previous" accesskey="p" href="#Generated-Scanner">Generated Scanner</a>,
  1619. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  1620.  
  1621. </div>
  1622.  
  1623. <h2 class="chapter">10 Start Conditions</h2>
  1624.  
  1625. <p><a name="index-start-conditions-159"></a><code>flex</code> provides a mechanism for conditionally activating rules. 
  1626. Any rule whose pattern is prefixed with `<samp><span class="samp"><sc></span></samp>' will only be active
  1627. when the scanner is in the <dfn>start condition</dfn> named <code>sc</code>.  For
  1628. example,
  1629.  
  1630. <!-- proofread edit stopped here -->
  1631. <pre class="example"><pre class="verbatim">     
  1632.          <STRING>[^"]*        { /* eat up the string body ... */
  1633.                      ...
  1634.                      }
  1635. </pre>
  1636. </pre>
  1637.    <p>will be active only when the scanner is in the <code>STRING</code> start
  1638. condition, and
  1639.  
  1640.    <p><a name="index-start-conditions_002c-multiple-160"></a>
  1641. <pre class="example"><pre class="verbatim">     
  1642.          <INITIAL,STRING,QUOTE>\.        { /* handle an escape ... */
  1643.                      ...
  1644.                      }
  1645. </pre>
  1646. </pre>
  1647.    <p>will be active only when the current start condition is either
  1648. <code>INITIAL</code>, <code>STRING</code>, or <code>QUOTE</code>.
  1649.  
  1650.    <p><a name="index-start-conditions_002c-inclusive-v_002es_002e-exclusive-161"></a>Start conditions are declared in the definitions (first) section of the
  1651. input using unindented lines beginning with either `<samp><span class="samp">%s</span></samp>' or
  1652. `<samp><span class="samp">%x</span></samp>' followed by a list of names.  The former declares
  1653. <dfn>inclusive</dfn> start conditions, the latter <dfn>exclusive</dfn> start
  1654. conditions.  A start condition is activated using the <code>BEGIN</code>
  1655. action.  Until the next <code>BEGIN</code> action is executed, rules with the
  1656. given start condition will be active and rules with other start
  1657. conditions will be inactive.  If the start condition is inclusive, then
  1658. rules with no start conditions at all will also be active.  If it is
  1659. exclusive, then <em>only</em> rules qualified with the start condition
  1660. will be active.  A set of rules contingent on the same exclusive start
  1661. condition describe a scanner which is independent of any of the other
  1662. rules in the <code>flex</code> input.  Because of this, exclusive start
  1663. conditions make it easy to specify “mini-scanners” which scan portions
  1664. of the input that are syntactically different from the rest (e.g.,
  1665. comments).
  1666.  
  1667.    <p>If the distinction between inclusive and exclusive start conditions
  1668. is still a little vague, here's a simple example illustrating the
  1669. connection between the two.  The set of rules:
  1670.  
  1671.    <p><a name="index-start-conditions_002c-inclusive-162"></a>
  1672. <pre class="example"><pre class="verbatim">     
  1673.          %s example
  1674.          %%
  1675.      
  1676.          <example>foo   do_something();
  1677.      
  1678.          bar            something_else();
  1679. </pre>
  1680. </pre>
  1681.    <p>is equivalent to
  1682.  
  1683.    <p><a name="index-start-conditions_002c-exclusive-163"></a>
  1684. <pre class="example"><pre class="verbatim">     
  1685.          %x example
  1686.          %%
  1687.      
  1688.          <example>foo   do_something();
  1689.      
  1690.          <INITIAL,example>bar    something_else();
  1691. </pre>
  1692. </pre>
  1693.    <p>Without the <code><INITIAL,example></code> qualifier, the <code>bar</code> pattern in
  1694. the second example wouldn't be active (i.e., couldn't match) when in
  1695. start condition <code>example</code>.  If we just used <code>example></code> to
  1696. qualify <code>bar</code>, though, then it would only be active in
  1697. <code>example</code> and not in <code>INITIAL</code>, while in the first example
  1698. it's active in both, because in the first example the <code>example</code>
  1699. start condition is an inclusive <code>(%s)</code> start condition.
  1700.  
  1701.    <p><a name="index-start-conditions_002c-special-wildcard-condition-164"></a>Also note that the special start-condition specifier
  1702. <code><*></code>
  1703. matches every start condition.  Thus, the above example could also
  1704. have been written:
  1705.  
  1706.    <p><a name="index-start-conditions_002c-use-of-wildcard-condition-_0028_003c_002a_003e_0029-165"></a>
  1707. <pre class="example"><pre class="verbatim">     
  1708.          %x example
  1709.          %%
  1710.      
  1711.          <example>foo   do_something();
  1712.      
  1713.          <*>bar    something_else();
  1714. </pre>
  1715. </pre>
  1716.    <p>The default rule (to <code>ECHO</code> any unmatched character) remains active
  1717. in start conditions.  It is equivalent to:
  1718.  
  1719.    <p><a name="index-start-conditions_002c-behavior-of-default-rule-166"></a>
  1720. <pre class="example"><pre class="verbatim">     
  1721.          <*>.|\n     ECHO;
  1722. </pre>
  1723. </pre>
  1724.    <p><a name="index-BEGIN_002c-explanation-167"></a><a name="index-BEGIN-168"></a><a name="index-INITIAL-169"></a><code>BEGIN(0)</code> returns to the original state where only the rules with
  1725. no start conditions are active.  This state can also be referred to as
  1726. the start-condition <code>INITIAL</code>, so <code>BEGIN(INITIAL)</code> is
  1727. equivalent to <code>BEGIN(0)</code>.  (The parentheses around the start
  1728. condition name are not required but are considered good style.)
  1729.  
  1730.    <p><code>BEGIN</code> actions can also be given as indented code at the beginning
  1731. of the rules section.  For example, the following will cause the scanner
  1732. to enter the <code>SPECIAL</code> start condition whenever <code>yylex()</code> is
  1733. called and the global variable <code>enter_special</code> is true:
  1734.  
  1735.    <p><a name="index-start-conditions_002c-using-BEGIN-170"></a>
  1736. <pre class="example"><pre class="verbatim">     
  1737.                  int enter_special;
  1738.      
  1739.          %x SPECIAL
  1740.          %%
  1741.                  if ( enter_special )
  1742.                      BEGIN(SPECIAL);
  1743.      
  1744.          <SPECIAL>blahblahblah
  1745.          ...more rules follow...
  1746. </pre>
  1747. </pre>
  1748.    <p>To illustrate the uses of start conditions, here is a scanner which
  1749. provides two different interpretations of a string like `<samp><span class="samp">123.456</span></samp>'. 
  1750. By default it will treat it as three tokens, the integer `<samp><span class="samp">123</span></samp>', a
  1751. dot (`<samp><span class="samp">.</span></samp>'), and the integer `<samp><span class="samp">456</span></samp>'.  But if the string is
  1752. preceded earlier in the line by the string `<samp><span class="samp">expect-floats</span></samp>' it will
  1753. treat it as a single token, the floating-point number `<samp><span class="samp">123.456</span></samp>':
  1754.  
  1755.    <p><a name="index-start-conditions_002c-for-different-interpretations-of-same-input-171"></a>
  1756. <pre class="example"><pre class="verbatim">     
  1757.          %{
  1758.          #include <math.h>
  1759.          %}
  1760.          %s expect
  1761.      
  1762.          %%
  1763.          expect-floats        BEGIN(expect);
  1764.      
  1765.          <expect>[0-9]+@samp{.}[0-9]+      {
  1766.                      printf( "found a float, = %f\n",
  1767.                              atof( yytext ) );
  1768.                      }
  1769.          <expect>\n           {
  1770.                      /* that's the end of the line, so
  1771.                       * we need another "expect-number"
  1772.                       * before we'll recognize any more
  1773.                       * numbers
  1774.                       */
  1775.                      BEGIN(INITIAL);
  1776.                      }
  1777.      
  1778.          [0-9]+      {
  1779.                      printf( "found an integer, = %d\n",
  1780.                              atoi( yytext ) );
  1781.                      }
  1782.      
  1783.          "."         printf( "found a dot\n" );
  1784. </pre>
  1785. </pre>
  1786.    <p><a name="index-comments_002c-example-of-scanning-C-comments-172"></a>Here is a scanner which recognizes (and discards) C comments while
  1787. maintaining a count of the current input line.
  1788.  
  1789.    <p><a name="index-recognizing-C-comments-173"></a>
  1790. <pre class="example"><pre class="verbatim">     
  1791.          %x comment
  1792.          %%
  1793.                  int line_num = 1;
  1794.      
  1795.          "/*"         BEGIN(comment);
  1796.      
  1797.          <comment>[^*\n]*        /* eat anything that's not a '*' */
  1798.          <comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
  1799.          <comment>\n             ++line_num;
  1800.          <comment>"*"+"/"        BEGIN(INITIAL);
  1801. </pre>
  1802. </pre>
  1803.    <p>This scanner goes to a bit of trouble to match as much
  1804. text as possible with each rule.  In general, when attempting to write
  1805. a high-speed scanner try to match as much possible in each rule, as
  1806. it's a big win.
  1807.  
  1808.    <p>Note that start-conditions names are really integer values and
  1809. can be stored as such.  Thus, the above could be extended in the
  1810. following fashion:
  1811.  
  1812.    <p><a name="index-start-conditions_002c-integer-values-174"></a><a name="index-using-integer-values-of-start-condition-names-175"></a>
  1813. <pre class="example"><pre class="verbatim">     
  1814.          %x comment foo
  1815.          %%
  1816.                  int line_num = 1;
  1817.                  int comment_caller;
  1818.      
  1819.          "/*"         {
  1820.                       comment_caller = INITIAL;
  1821.                       BEGIN(comment);
  1822.                       }
  1823.      
  1824.          ...
  1825.      
  1826.          <foo>"/*"    {
  1827.                       comment_caller = foo;
  1828.                       BEGIN(comment);
  1829.                       }
  1830.      
  1831.          <comment>[^*\n]*        /* eat anything that's not a '*' */
  1832.          <comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
  1833.          <comment>\n             ++line_num;
  1834.          <comment>"*"+"/"        BEGIN(comment_caller);
  1835. </pre>
  1836. </pre>
  1837.    <p><a name="index-YY_005fSTART_002c-example-176"></a>Furthermore, you can access the current start condition using the
  1838. integer-valued <code>YY_START</code> macro.  For example, the above
  1839. assignments to <code>comment_caller</code> could instead be written
  1840.  
  1841.    <p><a name="index-getting-current-start-state-with-YY_005fSTART-177"></a>
  1842. <pre class="example"><pre class="verbatim">     
  1843.          comment_caller = YY_START;
  1844. </pre>
  1845. </pre>
  1846.    <p><a name="index-YY_005fSTART-178"></a>Flex provides <code>YYSTATE</code> as an alias for <code>YY_START</code> (since that
  1847. is what's used by AT&T <code>lex</code>).
  1848.  
  1849.    <p>For historical reasons, start conditions do not have their own
  1850. name-space within the generated scanner. The start condition names are
  1851. unmodified in the generated scanner and generated header. 
  1852. See <a href="#option_002dheader">option-header</a>. See <a href="#option_002dprefix">option-prefix</a>.
  1853.  
  1854.    <p>Finally, here's an example of how to match C-style quoted strings using
  1855. exclusive start conditions, including expanded escape sequences (but
  1856. not including checking for a string that's too long):
  1857.  
  1858.    <p><a name="index-matching-C_002dstyle-double_002dquoted-strings-179"></a>
  1859. <pre class="example"><pre class="verbatim">     
  1860.          %x str
  1861.      
  1862.          %%
  1863.                  char string_buf[MAX_STR_CONST];
  1864.                  char *string_buf_ptr;
  1865.      
  1866.      
  1867.          \"      string_buf_ptr = string_buf; BEGIN(str);
  1868.      
  1869.          <str>\"        { /* saw closing quote - all done */
  1870.                  BEGIN(INITIAL);
  1871.                  *string_buf_ptr = '\0';
  1872.                  /* return string constant token type and
  1873.                   * value to parser
  1874.                   */
  1875.                  }
  1876.      
  1877.          <str>\n        {
  1878.                  /* error - unterminated string constant */
  1879.                  /* generate error message */
  1880.                  }
  1881.      
  1882.          <str>\\[0-7]{1,3} {
  1883.                  /* octal escape sequence */
  1884.                  int result;
  1885.      
  1886.                  (void) sscanf( yytext + 1, "%o", &result );
  1887.      
  1888.                  if ( result > 0xff )
  1889.                          /* error, constant is out-of-bounds */
  1890.      
  1891.                  *string_buf_ptr++ = result;
  1892.                  }
  1893.      
  1894.          <str>\\[0-9]+ {
  1895.                  /* generate error - bad escape sequence; something
  1896.                   * like '\48' or '\0777777'
  1897.                   */
  1898.                  }
  1899.      
  1900.          <str>\\n  *string_buf_ptr++ = '\n';
  1901.          <str>\\t  *string_buf_ptr++ = '\t';
  1902.          <str>\\r  *string_buf_ptr++ = '\r';
  1903.          <str>\\b  *string_buf_ptr++ = '\b';
  1904.          <str>\\f  *string_buf_ptr++ = '\f';
  1905.      
  1906.          <str>\\(.|\n)  *string_buf_ptr++ = yytext[1];
  1907.      
  1908.          <str>[^\\\n\"]+        {
  1909.                  char *yptr = yytext;
  1910.      
  1911.                  while ( *yptr )
  1912.                          *string_buf_ptr++ = *yptr++;
  1913.                  }
  1914. </pre>
  1915. </pre>
  1916.    <p><a name="index-start-condition_002c-applying-to-multiple-patterns-180"></a>Often, such as in some of the examples above, you wind up writing a
  1917. whole bunch of rules all preceded by the same start condition(s).  Flex
  1918. makes this a little easier and cleaner by introducing a notion of start
  1919. condition <dfn>scope</dfn>.  A start condition scope is begun with:
  1920.  
  1921. <pre class="example"><pre class="verbatim">     
  1922.          <SCs>{
  1923. </pre>
  1924. </pre>
  1925.    <p>where <code>SCs</code> is a list of one or more start conditions.  Inside the
  1926. start condition scope, every rule automatically has the prefix
  1927. <code>SCs></code> applied to it, until a `<samp><span class="samp">}</span></samp>' which matches the initial
  1928. `<samp><span class="samp">{</span></samp>'.  So, for example,
  1929.  
  1930.    <p><a name="index-extended-scope-of-start-conditions-181"></a>
  1931. <pre class="example"><pre class="verbatim">     
  1932.          <ESC>{
  1933.              "\\n"   return '\n';
  1934.              "\\r"   return '\r';
  1935.              "\\f"   return '\f';
  1936.              "\\0"   return '\0';
  1937.          }
  1938. </pre>
  1939. </pre>
  1940.    <p>is equivalent to:
  1941.  
  1942. <pre class="example"><pre class="verbatim">     
  1943.          <ESC>"\\n"  return '\n';
  1944.          <ESC>"\\r"  return '\r';
  1945.          <ESC>"\\f"  return '\f';
  1946.          <ESC>"\\0"  return '\0';
  1947. </pre>
  1948. </pre>
  1949.    <p>Start condition scopes may be nested.
  1950.  
  1951.    <p><a name="index-stacks_002c-routines-for-manipulating-182"></a><a name="index-start-conditions_002c-use-of-a-stack-183"></a>
  1952. The following routines are available for manipulating stacks of start conditions:
  1953.  
  1954. <div class="defun">
  1955. — Function: void <b>yy_push_state</b> (<var> int </var><code>new_state</code> )<var><a name="index-yy_005fpush_005fstate-184"></a></var><br>
  1956. <blockquote><p>pushes the current start condition onto the top of the start condition
  1957. stack and switches to
  1958. <code>new_state</code>
  1959. as though you had used
  1960. <code>BEGIN new_state</code>
  1961. (recall that start condition names are also integers). 
  1962. </p></blockquote></div>
  1963.  
  1964. <div class="defun">
  1965. — Function: void <b>yy_pop_state</b> ()<var><a name="index-yy_005fpop_005fstate-185"></a></var><br>
  1966. <blockquote><p>pops the top of the stack and switches to it via
  1967. <code>BEGIN</code>. 
  1968. </p></blockquote></div>
  1969.  
  1970. <div class="defun">
  1971. — Function: int <b>yy_top_state</b> ()<var><a name="index-yy_005ftop_005fstate-186"></a></var><br>
  1972. <blockquote><p>returns the top of the stack without altering the stack's contents. 
  1973. </p></blockquote></div>
  1974.  
  1975.    <p><a name="index-memory_002c-for-start-condition-stacks-187"></a>The start condition stack grows dynamically and so has no built-in size
  1976. limitation.  If memory is exhausted, program execution aborts.
  1977.  
  1978.    <p>To use start condition stacks, your scanner must include a <code>%option
  1979. stack</code> directive (see <a href="#Scanner-Options">Scanner Options</a>).
  1980.  
  1981. <div class="node">
  1982. <p><hr>
  1983. <a name="Multiple-Input-Buffers"></a>
  1984. Next: <a rel="next" accesskey="n" href="#EOF">EOF</a>,
  1985. Previous: <a rel="previous" accesskey="p" href="#Start-Conditions">Start Conditions</a>,
  1986. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  1987.  
  1988. </div>
  1989.  
  1990. <h2 class="chapter">11 Multiple Input Buffers</h2>
  1991.  
  1992. <p><a name="index-multiple-input-streams-188"></a>Some scanners (such as those which support “include” files) require
  1993. reading from several input streams.  As <code>flex</code> scanners do a large
  1994. amount of buffering, one cannot control where the next input will be
  1995. read from by simply writing a <code>YY_INPUT()</code> which is sensitive to
  1996. the scanning context.  <code>YY_INPUT()</code> is only called when the scanner
  1997. reaches the end of its buffer, which may be a long time after scanning a
  1998. statement such as an <code>include</code> statement which requires switching
  1999. the input source.
  2000.  
  2001.    <p>To negotiate these sorts of problems, <code>flex</code> provides a mechanism
  2002. for creating and switching between multiple input buffers.  An input
  2003. buffer is created by using:
  2004.  
  2005.    <p><a name="index-memory_002c-allocating-input-buffers-189"></a>
  2006.  
  2007. <div class="defun">
  2008. — Function: YY_BUFFER_STATE <b>yy_create_buffer</b> (<var> FILE *file, int size </var>)<var><a name="index-yy_005fcreate_005fbuffer-190"></a></var><br>
  2009. </div>
  2010.  
  2011.    <p>which takes a <code>FILE</code> pointer and a size and creates a buffer
  2012. associated with the given file and large enough to hold <code>size</code>
  2013. characters (when in doubt, use <code>YY_BUF_SIZE</code> for the size).  It
  2014. returns a <code>YY_BUFFER_STATE</code> handle, which may then be passed to
  2015. other routines (see below). 
  2016. <a name="index-YY_005fBUFFER_005fSTATE-191"></a>The <code>YY_BUFFER_STATE</code> type is a
  2017. pointer to an opaque <code>struct yy_buffer_state</code> structure, so you may
  2018. safely initialize <code>YY_BUFFER_STATE</code> variables to <code>((YY_BUFFER_STATE)
  2019. 0)</code> if you wish, and also refer to the opaque structure in order to
  2020. correctly declare input buffers in source files other than that of your
  2021. scanner.  Note that the <code>FILE</code> pointer in the call to
  2022. <code>yy_create_buffer</code> is only used as the value of <samp><span class="file">yyin</span></samp> seen by
  2023. <code>YY_INPUT</code>.  If you redefine <code>YY_INPUT()</code> so it no longer uses
  2024. <samp><span class="file">yyin</span></samp>, then you can safely pass a NULL <code>FILE</code> pointer to
  2025. <code>yy_create_buffer</code>.  You select a particular buffer to scan from
  2026. using:
  2027.  
  2028. <div class="defun">
  2029. — Function: void <b>yy_switch_to_buffer</b> (<var> YY_BUFFER_STATE new_buffer </var>)<var><a name="index-yy_005fswitch_005fto_005fbuffer-192"></a></var><br>
  2030. </div>
  2031.  
  2032.    <p>The above function switches the scanner's input buffer so subsequent tokens
  2033. will come from <code>new_buffer</code>.  Note that <code>yy_switch_to_buffer()</code> may
  2034. be used by <code>yywrap()</code> to set things up for continued scanning, instead of
  2035. opening a new file and pointing <samp><span class="file">yyin</span></samp> at it. If you are looking for a
  2036. stack of input buffers, then you want to use <code>yypush_buffer_state()</code>
  2037. instead of this function. Note also that switching input sources via either
  2038. <code>yy_switch_to_buffer()</code> or <code>yywrap()</code> does <em>not</em> change the
  2039. start condition.
  2040.  
  2041.    <p><a name="index-memory_002c-deleting-input-buffers-193"></a>
  2042.  
  2043. <div class="defun">
  2044. — Function: void <b>yy_delete_buffer</b> (<var> YY_BUFFER_STATE buffer </var>)<var><a name="index-yy_005fdelete_005fbuffer-194"></a></var><br>
  2045. </div>
  2046.  
  2047.    <p>is used to reclaim the storage associated with a buffer.  (<code>buffer</code>
  2048. can be NULL, in which case the routine does nothing.)  You can also clear
  2049. the current contents of a buffer using:
  2050.  
  2051.    <p><a name="index-pushing-an-input-buffer-195"></a><a name="index-stack_002c-input-buffer-push-196"></a>
  2052.  
  2053. <div class="defun">
  2054. — Function: void <b>yypush_buffer_state</b> (<var> YY_BUFFER_STATE buffer </var>)<var><a name="index-yypush_005fbuffer_005fstate-197"></a></var><br>
  2055. </div>
  2056.  
  2057.    <p>This function pushes the new buffer state onto an internal stack. The pushed
  2058. state becomes the new current state. The stack is maintained by flex and will
  2059. grow as required. This function is intended to be used instead of
  2060. <code>yy_switch_to_buffer</code>, when you want to change states, but preserve the
  2061. current state for later use.
  2062.  
  2063.    <p><a name="index-popping-an-input-buffer-198"></a><a name="index-stack_002c-input-buffer-pop-199"></a>
  2064.  
  2065. <div class="defun">
  2066. — Function: void <b>yypop_buffer_state</b> ( )<var><a name="index-yypop_005fbuffer_005fstate-200"></a></var><br>
  2067. </div>
  2068.  
  2069.    <p>This function removes the current state from the top of the stack, and deletes
  2070. it by calling <code>yy_delete_buffer</code>.  The next state on the stack, if any,
  2071. becomes the new current state.
  2072.  
  2073.    <p><a name="index-clearing-an-input-buffer-201"></a><a name="index-flushing-an-input-buffer-202"></a>
  2074.  
  2075. <div class="defun">
  2076. — Function: void <b>yy_flush_buffer</b> (<var> YY_BUFFER_STATE buffer </var>)<var><a name="index-yy_005fflush_005fbuffer-203"></a></var><br>
  2077. </div>
  2078.  
  2079.    <p>This function discards the buffer's contents,
  2080. so the next time the scanner attempts to match a token from the
  2081. buffer, it will first fill the buffer anew using
  2082. <code>YY_INPUT()</code>.
  2083.  
  2084. <div class="defun">
  2085. — Function: YY_BUFFER_STATE <b>yy_new_buffer</b> (<var> FILE *file, int size </var>)<var><a name="index-yy_005fnew_005fbuffer-204"></a></var><br>
  2086. </div>
  2087.  
  2088.    <p>is an alias for <code>yy_create_buffer()</code>,
  2089. provided for compatibility with the C++ use of <code>new</code> and
  2090. <code>delete</code> for creating and destroying dynamic objects.
  2091.  
  2092.    <p><a name="index-YY_005fCURRENT_005fBUFFER_002c-and-multiple-buffers-Finally_002c-the-macro-205"></a><code>YY_CURRENT_BUFFER</code> macro returns a <code>YY_BUFFER_STATE</code> handle to the
  2093. current buffer. It should not be used as an lvalue.
  2094.  
  2095.    <p><a name="index-EOF_002c-example-using-multiple-input-buffers-206"></a>Here are two examples of using these features for writing a scanner
  2096. which expands include files (the
  2097. <code><<EOF>></code>
  2098. feature is discussed below).
  2099.  
  2100.    <p>This first example uses yypush_buffer_state and yypop_buffer_state. Flex
  2101. maintains the stack internally.
  2102.  
  2103.    <p><a name="index-handling-include-files-with-multiple-input-buffers-207"></a>
  2104. <pre class="example"><pre class="verbatim">     
  2105.          /* the "incl" state is used for picking up the name
  2106.           * of an include file
  2107.           */
  2108.          %x incl
  2109.          %%
  2110.          include             BEGIN(incl);
  2111.      
  2112.          [a-z]+              ECHO;
  2113.          [^a-z\n]*\n?        ECHO;
  2114.      
  2115.          <incl>[ \t]*      /* eat the whitespace */
  2116.          <incl>[^ \t\n]+   { /* got the include file name */
  2117.                  yyin = fopen( yytext, "r" );
  2118.      
  2119.                  if ( ! yyin )
  2120.                      error( ... );
  2121.      
  2122.                  yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE ));
  2123.      
  2124.                  BEGIN(INITIAL);
  2125.                  }
  2126.      
  2127.          <<EOF>> {
  2128.                  yypop_buffer_state();
  2129.      
  2130.                  if ( !YY_CURRENT_BUFFER )
  2131.                      {
  2132.                      yyterminate();
  2133.                      }
  2134.                  }
  2135. </pre>
  2136. </pre>
  2137.    <p>The second example, below, does the same thing as the previous example did, but
  2138. manages its own input buffer stack manually (instead of letting flex do it).
  2139.  
  2140.    <p><a name="index-handling-include-files-with-multiple-input-buffers-208"></a>
  2141. <pre class="example"><pre class="verbatim">     
  2142.          /* the "incl" state is used for picking up the name
  2143.           * of an include file
  2144.           */
  2145.          %x incl
  2146.      
  2147.          %{
  2148.          #define MAX_INCLUDE_DEPTH 10
  2149.          YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  2150.          int include_stack_ptr = 0;
  2151.          %}
  2152.      
  2153.          %%
  2154.          include             BEGIN(incl);
  2155.      
  2156.          [a-z]+              ECHO;
  2157.          [^a-z\n]*\n?        ECHO;
  2158.      
  2159.          <incl>[ \t]*      /* eat the whitespace */
  2160.          <incl>[^ \t\n]+   { /* got the include file name */
  2161.                  if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
  2162.                      {
  2163.                      fprintf( stderr, "Includes nested too deeply" );
  2164.                      exit( 1 );
  2165.                      }
  2166.      
  2167.                  include_stack[include_stack_ptr++] =
  2168.                      YY_CURRENT_BUFFER;
  2169.      
  2170.                  yyin = fopen( yytext, "r" );
  2171.      
  2172.                  if ( ! yyin )
  2173.                      error( ... );
  2174.      
  2175.                  yy_switch_to_buffer(
  2176.                      yy_create_buffer( yyin, YY_BUF_SIZE ) );
  2177.      
  2178.                  BEGIN(INITIAL);
  2179.                  }
  2180.      
  2181.          <<EOF>> {
  2182.                  if ( --include_stack_ptr  0 )
  2183.                      {
  2184.                      yyterminate();
  2185.                      }
  2186.      
  2187.                  else
  2188.                      {
  2189.                      yy_delete_buffer( YY_CURRENT_BUFFER );
  2190.                      yy_switch_to_buffer(
  2191.                           include_stack[include_stack_ptr] );
  2192.                      }
  2193.                  }
  2194. </pre>
  2195. </pre>
  2196.    <p><a name="Scanning-Strings"></a>
  2197. <a name="index-strings_002c-scanning-strings-instead-of-files-209"></a>The following routines are available for setting up input buffers for
  2198. scanning in-memory strings instead of files.  All of them create a new
  2199. input buffer for scanning the string, and return a corresponding
  2200. <code>YY_BUFFER_STATE</code> handle (which you should delete with
  2201. <code>yy_delete_buffer()</code> when done with it).  They also switch to the
  2202. new buffer using <code>yy_switch_to_buffer()</code>, so the next call to
  2203. <code>yylex()</code> will start scanning the string.
  2204.  
  2205. <div class="defun">
  2206. — Function: YY_BUFFER_STATE <b>yy_scan_string</b> (<var> const char *str </var>)<var><a name="index-yy_005fscan_005fstring-210"></a></var><br>
  2207. <blockquote><p>scans a NUL-terminated string. 
  2208. </p></blockquote></div>
  2209.  
  2210. <div class="defun">
  2211. — Function: YY_BUFFER_STATE <b>yy_scan_bytes</b> (<var> const char *bytes, int len </var>)<var><a name="index-yy_005fscan_005fbytes-211"></a></var><br>
  2212. <blockquote><p>scans <code>len</code> bytes (including possibly <code>NUL</code>s) starting at location
  2213. <code>bytes</code>. 
  2214. </p></blockquote></div>
  2215.  
  2216.    <p>Note that both of these functions create and scan a <em>copy</em> of the
  2217. string or bytes.  (This may be desirable, since <code>yylex()</code> modifies
  2218. the contents of the buffer it is scanning.)  You can avoid the copy by
  2219. using:
  2220.  
  2221.    <p><a name="index-YY_005fEND_005fOF_005fBUFFER_005fCHAR-212"></a>
  2222.  
  2223. <div class="defun">
  2224. — Function: YY_BUFFER_STATE <b>yy_scan_buffer</b> (<var>char *base, yy_size_t size</var>)<var><a name="index-yy_005fscan_005fbuffer-213"></a></var><br>
  2225. <blockquote><p>which scans in place the buffer starting at <code>base</code>, consisting of
  2226. <code>size</code> bytes, the last two bytes of which <em>must</em> be
  2227. <code>YY_END_OF_BUFFER_CHAR</code> (ASCII NUL).  These last two bytes are not
  2228. scanned; thus, scanning consists of <code>base[0]</code> through
  2229. <code>base[size-2]</code>, inclusive. 
  2230. </p></blockquote></div>
  2231.  
  2232.    <p>If you fail to set up <code>base</code> in this manner (i.e., forget the final
  2233. two <code>YY_END_OF_BUFFER_CHAR</code> bytes), then <code>yy_scan_buffer()</code>
  2234. returns a NULL pointer instead of creating a new input buffer.
  2235.  
  2236. <div class="defun">
  2237. — Data type: <b>yy_size_t</b><var><a name="index-yy_005fsize_005ft-214"></a></var><br>
  2238. <blockquote><p>is an integral type to which you can cast an integer expression
  2239. reflecting the size of the buffer. 
  2240. </p></blockquote></div>
  2241.  
  2242. <div class="node">
  2243. <p><hr>
  2244. <a name="EOF"></a>
  2245. Next: <a rel="next" accesskey="n" href="#Misc-Macros">Misc Macros</a>,
  2246. Previous: <a rel="previous" accesskey="p" href="#Multiple-Input-Buffers">Multiple Input Buffers</a>,
  2247. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2248.  
  2249. </div>
  2250.  
  2251. <h2 class="chapter">12 End-of-File Rules</h2>
  2252.  
  2253. <p><a name="index-EOF_002c-explanation-215"></a>The special rule <code><<EOF>></code> indicates
  2254. actions which are to be taken when an end-of-file is
  2255. encountered and <code>yywrap()</code> returns non-zero (i.e., indicates
  2256. no further files to process).  The action must finish
  2257. by doing one of the following things:
  2258.  
  2259.      <ul>
  2260. <li><a name="index-YY_005fNEW_005fFILE--_0028now-obsolete_0029-216"></a>assigning <samp><span class="file">yyin</span></samp> to a new input file (in previous versions of
  2261. <code>flex</code>, after doing the assignment you had to call the special
  2262. action <code>YY_NEW_FILE</code>.  This is no longer necessary.)
  2263.  
  2264.      <li>executing a <code>return</code> statement;
  2265.  
  2266.      <li>executing the special <code>yyterminate()</code> action.
  2267.  
  2268.      <li>or, switching to a new buffer using <code>yy_switch_to_buffer()</code> as
  2269. shown in the example above. 
  2270. </ul>
  2271.  
  2272.    <p><<EOF>> rules may not be used with other patterns; they may only be
  2273. qualified with a list of start conditions.  If an unqualified <<EOF>>
  2274. rule is given, it applies to <em>all</em> start conditions which do not
  2275. already have <<EOF>> actions.  To specify an <<EOF>> rule for only the
  2276. initial start condition, use:
  2277.  
  2278. <pre class="example"><pre class="verbatim">     
  2279.          <INITIAL><<EOF>>
  2280. </pre>
  2281. </pre>
  2282.    <p>These rules are useful for catching things like unclosed comments.  An
  2283. example:
  2284.  
  2285.    <p><a name="index-g_t_003c_003cEOF_003e_003e_002c-use-of-217"></a>
  2286. <pre class="example"><pre class="verbatim">     
  2287.          %x quote
  2288.          %%
  2289.      
  2290.          ...other rules for dealing with quotes...
  2291.      
  2292.          <quote><<EOF>>   {
  2293.                   error( "unterminated quote" );
  2294.                   yyterminate();
  2295.                   }
  2296.         <<EOF>>  {
  2297.                   if ( *++filelist )
  2298.                       yyin = fopen( *filelist, "r" );
  2299.                   else
  2300.                      yyterminate();
  2301.                   }
  2302. </pre>
  2303. </pre>
  2304.    <div class="node">
  2305. <p><hr>
  2306. <a name="Misc-Macros"></a>
  2307. Next: <a rel="next" accesskey="n" href="#User-Values">User Values</a>,
  2308. Previous: <a rel="previous" accesskey="p" href="#EOF">EOF</a>,
  2309. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2310.  
  2311. </div>
  2312.  
  2313. <h2 class="chapter">13 Miscellaneous Macros</h2>
  2314.  
  2315. <p><a name="index-YY_005fUSER_005fACTION-218"></a>The macro <code>YY_USER_ACTION</code> can be defined to provide an action
  2316. which is always executed prior to the matched rule's action.  For
  2317. example, it could be #define'd to call a routine to convert yytext to
  2318. lower-case.  When <code>YY_USER_ACTION</code> is invoked, the variable
  2319. <code>yy_act</code> gives the number of the matched rule (rules are numbered
  2320. starting with 1).  Suppose you want to profile how often each of your
  2321. rules is matched.  The following would do the trick:
  2322.  
  2323.    <p><a name="index-YY_005fUSER_005fACTION-to-track-each-time-a-rule-is-matched-219"></a>
  2324. <pre class="example"><pre class="verbatim">     
  2325.          #define YY_USER_ACTION ++ctr[yy_act]
  2326. </pre>
  2327. </pre>
  2328.    <p><a name="index-YY_005fNUM_005fRULES-220"></a>where <code>ctr</code> is an array to hold the counts for the different rules. 
  2329. Note that the macro <code>YY_NUM_RULES</code> gives the total number of rules
  2330. (including the default rule), even if you use `<samp><span class="samp">-s)</span></samp>', so a correct
  2331. declaration for <code>ctr</code> is:
  2332.  
  2333. <pre class="example"><pre class="verbatim">     
  2334.          int ctr[YY_NUM_RULES];
  2335. </pre>
  2336. </pre>
  2337.    <p><a name="index-YY_005fUSER_005fINIT-221"></a>The macro <code>YY_USER_INIT</code> may be defined to provide an action which
  2338. is always executed before the first scan (and before the scanner's
  2339. internal initializations are done).  For example, it could be used to
  2340. call a routine to read in a data table or open a logging file.
  2341.  
  2342.    <p><a name="index-yy_005fset_005finteractive-222"></a>The macro <code>yy_set_interactive(is_interactive)</code> can be used to
  2343. control whether the current buffer is considered <dfn>interactive</dfn>.  An
  2344. interactive buffer is processed more slowly, but must be used when the
  2345. scanner's input source is indeed interactive to avoid problems due to
  2346. waiting to fill buffers (see the discussion of the `<samp><span class="samp">-I</span></samp>' flag in
  2347. <a href="#Scanner-Options">Scanner Options</a>).  A non-zero value in the macro invocation marks
  2348. the buffer as interactive, a zero value as non-interactive.  Note that
  2349. use of this macro overrides <code>%option always-interactive</code> or
  2350. <code>%option never-interactive</code> (see <a href="#Scanner-Options">Scanner Options</a>). 
  2351. <code>yy_set_interactive()</code> must be invoked prior to beginning to scan
  2352. the buffer that is (or is not) to be considered interactive.
  2353.  
  2354.    <p><a name="index-BOL_002c-setting-it-223"></a><a name="index-yy_005fset_005fbol-224"></a>The macro <code>yy_set_bol(at_bol)</code> can be used to control whether the
  2355. current buffer's scanning context for the next token match is done as
  2356. though at the beginning of a line.  A non-zero macro argument makes
  2357. rules anchored with `<samp><span class="samp">^</span></samp>' active, while a zero argument makes
  2358. `<samp><span class="samp">^</span></samp>' rules inactive.
  2359.  
  2360.    <p><a name="index-BOL_002c-checking-the-BOL-flag-225"></a><a name="index-YY_005fAT_005fBOL-226"></a>The macro <code>YY_AT_BOL()</code> returns true if the next token scanned from
  2361. the current buffer will have `<samp><span class="samp">^</span></samp>' rules active, false otherwise.
  2362.  
  2363.    <p><a name="index-actions_002c-redefining-YY_005fBREAK-227"></a><a name="index-YY_005fBREAK-228"></a>In the generated scanner, the actions are all gathered in one large
  2364. switch statement and separated using <code>YY_BREAK</code>, which may be
  2365. redefined.  By default, it is simply a <code>break</code>, to separate each
  2366. rule's action from the following rule's.  Redefining <code>YY_BREAK</code>
  2367. allows, for example, C++ users to #define YY_BREAK to do nothing (while
  2368. being very careful that every rule ends with a <code>break</code>" or a
  2369. <code>return</code>!) to avoid suffering from unreachable statement warnings
  2370. where because a rule's action ends with <code>return</code>, the
  2371. <code>YY_BREAK</code> is inaccessible.
  2372.  
  2373. <div class="node">
  2374. <p><hr>
  2375. <a name="User-Values"></a>
  2376. Next: <a rel="next" accesskey="n" href="#Yacc">Yacc</a>,
  2377. Previous: <a rel="previous" accesskey="p" href="#Misc-Macros">Misc Macros</a>,
  2378. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2379.  
  2380. </div>
  2381.  
  2382. <h2 class="chapter">14 Values Available To the User</h2>
  2383.  
  2384. <p>This chapter summarizes the various values available to the user in the
  2385. rule actions.
  2386.  
  2387.      
  2388. <a name="index-yytext-229"></a>
  2389. <dl><dt><code>char *yytext</code><dd>holds the text of the current token.  It may be modified but not
  2390. lengthened (you cannot append characters to the end).
  2391.  
  2392.      <p><a name="index-yytext_002c-default-array-size-230"></a><a name="index-array_002c-default-size-for-yytext-231"></a><a name="index-YYLMAX-232"></a>If the special directive <code>%array</code> appears in the first section of
  2393. the scanner description, then <code>yytext</code> is instead declared
  2394. <code>char yytext[YYLMAX]</code>, where <code>YYLMAX</code> is a macro definition
  2395. that you can redefine in the first section if you don't like the default
  2396. value (generally 8KB).  Using <code>%array</code> results in somewhat slower
  2397. scanners, but the value of <code>yytext</code> becomes immune to calls to
  2398. <code>unput()</code>, which potentially destroy its value when <code>yytext</code> is
  2399. a character pointer.  The opposite of <code>%array</code> is <code>%pointer</code>,
  2400. which is the default.
  2401.  
  2402.      <p><a name="index-C_002b_002b-and-_0025array-233"></a>You cannot use <code>%array</code> when generating C++ scanner classes (the
  2403. `<samp><span class="samp">-+</span></samp>' flag).
  2404.  
  2405.      <p><a name="index-yyleng-234"></a><br><dt><code>int yyleng</code><dd>holds the length of the current token.
  2406.  
  2407.      <p><a name="index-yyin-235"></a><br><dt><code>FILE *yyin</code><dd>is the file which by default <code>flex</code> reads from.  It may be
  2408. redefined but doing so only makes sense before scanning begins or after
  2409. an EOF has been encountered.  Changing it in the midst of scanning will
  2410. have unexpected results since <code>flex</code> buffers its input; use
  2411. <code>yyrestart()</code> instead.  Once scanning terminates because an
  2412. end-of-file has been seen, you can assign <samp><span class="file">yyin</span></samp> at the new input
  2413. file and then call the scanner again to continue scanning.
  2414.  
  2415.      <p><a name="index-yyrestart-236"></a><br><dt><code>void yyrestart( FILE *new_file )</code><dd>may be called to point <samp><span class="file">yyin</span></samp> at the new input file.  The
  2416. switch-over to the new file is immediate (any previously buffered-up
  2417. input is lost).  Note that calling <code>yyrestart()</code> with <samp><span class="file">yyin</span></samp>
  2418. as an argument thus throws away the current input buffer and continues
  2419. scanning the same input file.
  2420.  
  2421.      <p><a name="index-yyout-237"></a><br><dt><code>FILE *yyout</code><dd>is the file to which <code>ECHO</code> actions are done.  It can be reassigned
  2422. by the user.
  2423.  
  2424.      <p><a name="index-YY_005fCURRENT_005fBUFFER-238"></a><br><dt><code>YY_CURRENT_BUFFER</code><dd>returns a <code>YY_BUFFER_STATE</code> handle to the current buffer.
  2425.  
  2426.      <p><a name="index-YY_005fSTART-239"></a><br><dt><code>YY_START</code><dd>returns an integer value corresponding to the current start condition. 
  2427. You can subsequently use this value with <code>BEGIN</code> to return to that
  2428. start condition. 
  2429. </dl>
  2430.  
  2431. <div class="node">
  2432. <p><hr>
  2433. <a name="Yacc"></a>
  2434. Next: <a rel="next" accesskey="n" href="#Scanner-Options">Scanner Options</a>,
  2435. Previous: <a rel="previous" accesskey="p" href="#User-Values">User Values</a>,
  2436. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2437.  
  2438. </div>
  2439.  
  2440. <h2 class="chapter">15 Interfacing with Yacc</h2>
  2441.  
  2442. <p><a name="index-yacc_002c-interface-240"></a>
  2443. <a name="index-yylval_002c-with-yacc-241"></a>One of the main uses of <code>flex</code> is as a companion to the <code>yacc</code>
  2444. parser-generator.  <code>yacc</code> parsers expect to call a routine named
  2445. <code>yylex()</code> to find the next input token.  The routine is supposed to
  2446. return the type of the next token as well as putting any associated
  2447. value in the global <code>yylval</code>.  To use <code>flex</code> with <code>yacc</code>,
  2448. one specifies the `<samp><span class="samp">-d</span></samp>' option to <code>yacc</code> to instruct it to
  2449. generate the file <samp><span class="file">y.tab.h</span></samp> containing definitions of all the
  2450. <code>%tokens</code> appearing in the <code>yacc</code> input.  This file is then
  2451. included in the <code>flex</code> scanner.  For example, if one of the tokens
  2452. is <code>TOK_NUMBER</code>, part of the scanner might look like:
  2453.  
  2454.    <p><a name="index-yacc-interface-242"></a>
  2455. <pre class="example"><pre class="verbatim">     
  2456.          %{
  2457.          #include "y.tab.h"
  2458.          %}
  2459.      
  2460.          %%
  2461.      
  2462.          [0-9]+        yylval = atoi( yytext ); return TOK_NUMBER;
  2463. </pre>
  2464. </pre>
  2465.    <div class="node">
  2466. <p><hr>
  2467. <a name="Scanner-Options"></a>
  2468. Next: <a rel="next" accesskey="n" href="#Performance">Performance</a>,
  2469. Previous: <a rel="previous" accesskey="p" href="#Yacc">Yacc</a>,
  2470. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2471.  
  2472. </div>
  2473.  
  2474. <h2 class="chapter">16 Scanner Options</h2>
  2475.  
  2476. <p><a name="index-command_002dline-options-243"></a><a name="index-options_002c-command_002dline-244"></a><a name="index-arguments_002c-command_002dline-245"></a>
  2477. The various <code>flex</code> options are categorized by function in the following
  2478. menu. If you want to lookup a particular option by name, See <a href="#Index-of-Scanner-Options">Index of Scanner Options</a>.
  2479.  
  2480. <ul class="menu">
  2481. <li><a accesskey="1" href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a>
  2482. <li><a accesskey="2" href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a>
  2483. <li><a accesskey="3" href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a>
  2484. <li><a accesskey="4" href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a>
  2485. <li><a accesskey="5" href="#Debugging-Options">Debugging Options</a>
  2486. <li><a accesskey="6" href="#Miscellaneous-Options">Miscellaneous Options</a>
  2487. </ul>
  2488.  
  2489.    <p>Even though there are many scanner options, a typical scanner might only
  2490. specify the following options:
  2491.  
  2492. <pre class="example"><pre class="verbatim">     
  2493.      %option   8bit reentrant bison-bridge
  2494.      %option   warn nodefault
  2495.      %option   yylineno
  2496.      %option   outfile="scanner.c" header-file="scanner.h"
  2497. </pre>
  2498. </pre>
  2499.    <p>The first line specifies the general type of scanner we want. The second line
  2500. specifies that we are being careful. The third line asks flex to track line
  2501. numbers. The last line tells flex what to name the files. (The options can be
  2502. specified in any order. We just dividied them.)
  2503.  
  2504.    <p><code>flex</code> also provides a mechanism for controlling options within the
  2505. scanner specification itself, rather than from the flex command-line. 
  2506. This is done by including <code>%option</code> directives in the first section
  2507. of the scanner specification.  You can specify multiple options with a
  2508. single <code>%option</code> directive, and multiple directives in the first
  2509. section of your flex input file.
  2510.  
  2511.    <p>Most options are given simply as names, optionally preceded by the
  2512. word `<samp><span class="samp">no</span></samp>' (with no intervening whitespace) to negate their meaning. 
  2513. The names are the same as their long-option equivalents (but without the
  2514. leading `<samp><span class="samp">--</span></samp>' ).
  2515.  
  2516.    <p><code>flex</code> scans your rule actions to determine whether you use the
  2517. <code>REJECT</code> or <code>yymore()</code> features.  The <code>REJECT</code> and
  2518. <code>yymore</code> options are available to override its decision as to
  2519. whether you use the options, either by setting them (e.g., <code>%option
  2520. reject)</code> to indicate the feature is indeed used, or unsetting them to
  2521. indicate it actually is not used (e.g., <code>%option noyymore)</code>.
  2522.  
  2523.    <p>A number of options are available for lint purists who want to suppress
  2524. the appearance of unneeded routines in the generated scanner.  Each of
  2525. the following, if unset (e.g., <code>%option nounput</code>), results in the
  2526. corresponding routine not appearing in the generated scanner:
  2527.  
  2528. <pre class="example"><pre class="verbatim">     
  2529.          input, unput
  2530.          yy_push_state, yy_pop_state, yy_top_state
  2531.          yy_scan_buffer, yy_scan_bytes, yy_scan_string
  2532.      
  2533.          yyget_extra, yyset_extra, yyget_leng, yyget_text,
  2534.          yyget_lineno, yyset_lineno, yyget_in, yyset_in,
  2535.          yyget_out, yyset_out, yyget_lval, yyset_lval,
  2536.          yyget_lloc, yyset_lloc, yyget_debug, yyset_debug
  2537. </pre>
  2538. </pre>
  2539.    <p>(though <code>yy_push_state()</code> and friends won't appear anyway unless
  2540. you use <code>%option stack)</code>.
  2541.  
  2542. <div class="node">
  2543. <p><hr>
  2544. <a name="Options-for-Specifing-Filenames"></a>
  2545. Next: <a rel="next" accesskey="n" href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a>,
  2546. Previous: <a rel="previous" accesskey="p" href="#Scanner-Options">Scanner Options</a>,
  2547. Up: <a rel="up" accesskey="u" href="#Scanner-Options">Scanner Options</a>
  2548.  
  2549. </div>
  2550.  
  2551. <h3 class="section">16.1 Options for Specifing Filenames</h3>
  2552.  
  2553.      <dl>
  2554. <a name="option_002dheader"></a>
  2555. <a name="index-g_t_002d_002d_002dheader_002dfile-246"></a><a name="index-header_002dfile-247"></a><dt>`<samp><span class="samp">--header-file=FILE, </span><code>%option header-file="FILE"</code></samp>'<dd>instructs flex to write a C header to <samp><span class="file">FILE</span></samp>. This file contains
  2556. function prototypes, extern variables, and types used by the scanner. 
  2557. Only the external API is exported by the header file. Many macros that
  2558. are usable from within scanner actions are not exported to the header
  2559. file. This is due to namespace problems and the goal of a clean
  2560. external API.
  2561.  
  2562.      <p>While in the header, the macro <code>yyIN_HEADER</code> is defined, where `<samp><span class="samp">yy</span></samp>'
  2563. is substituted with the appropriate prefix.
  2564.  
  2565.      <p>The `<samp><span class="samp">--header-file</span></samp>' option is not compatible with the `<samp><span class="samp">--c++</span></samp>' option,
  2566. since the C++ scanner provides its own header in <samp><span class="file">yyFlexLexer.h</span></samp>.
  2567.  
  2568.      <p><a name="option_002doutfile"></a>
  2569. <a name="index-g_t_002do-248"></a><a name="index-g_t_002d_002d_002doutfile-249"></a><a name="index-outfile-250"></a><br><dt>`<samp><span class="samp">-oFILE, --outfile=FILE, </span><code>%option outfile="FILE"</code></samp>'<dd>directs flex to write the scanner to the file <samp><span class="file">FILE</span></samp> instead of
  2570. <samp><span class="file">lex.yy.c</span></samp>.  If you combine `<samp><span class="samp">--outfile</span></samp>' with the `<samp><span class="samp">--stdout</span></samp>' option,
  2571. then the scanner is written to <samp><span class="file">stdout</span></samp> but its <code>#line</code>
  2572. directives (see the `<samp><span class="samp">-l</span></samp>' option above) refer to the file
  2573. <samp><span class="file">FILE</span></samp>.
  2574.  
  2575.      <p><a name="option_002dstdout"></a>
  2576. <a name="index-g_t_002dt-251"></a><a name="index-g_t_002d_002d_002dstdout-252"></a><a name="index-stdout-253"></a><br><dt>`<samp><span class="samp">-t, --stdout, </span><code>%option stdout</code></samp>'<dd>instructs <code>flex</code> to write the scanner it generates to standard
  2577. output instead of <samp><span class="file">lex.yy.c</span></samp>.
  2578.  
  2579.      <p><a name="index-g_t_002d_002d_002dskel-254"></a><br><dt>`<samp><span class="samp">-SFILE, --skel=FILE</span></samp>'<dd>overrides the default skeleton file from which
  2580. <code>flex</code>
  2581. constructs its scanners.  You'll never need this option unless you are doing
  2582. <code>flex</code>
  2583. maintenance or development.
  2584.  
  2585.      <p><a name="index-g_t_002d_002d_002dtables_002dfile-255"></a><a name="index-tables_002dfile-256"></a><br><dt>`<samp><span class="samp">--tables-file=FILE</span></samp>'<dd>Write serialized scanner dfa tables to FILE. The generated scanner will not
  2586. contain the tables, and requires them to be loaded at runtime. 
  2587. See <a href="#serialization">serialization</a>.
  2588.  
  2589.      <p><a name="index-g_t_002d_002d_002dtables_002dverify-257"></a><a name="index-tables_002dverify-258"></a><br><dt>`<samp><span class="samp">--tables-verify</span></samp>'<dd>This option is for flex development. We document it here in case you stumble
  2590. upon it by accident or in case you suspect some inconsistency in the serialized
  2591. tables.  Flex will serialize the scanner dfa tables but will also generate the
  2592. in-code tables as it normally does. At runtime, the scanner will verify that
  2593. the serialized tables match the in-code tables, instead of loading them.
  2594.  
  2595. </dl>
  2596.  
  2597. <div class="node">
  2598. <p><hr>
  2599. <a name="Options-Affecting-Scanner-Behavior"></a>
  2600. Next: <a rel="next" accesskey="n" href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a>,
  2601. Previous: <a rel="previous" accesskey="p" href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a>,
  2602. Up: <a rel="up" accesskey="u" href="#Scanner-Options">Scanner Options</a>
  2603.  
  2604. </div>
  2605.  
  2606. <h3 class="section">16.2 Options Affecting Scanner Behavior</h3>
  2607.  
  2608.      <dl>
  2609. <a name="option_002dcase_002dinsensitive"></a>
  2610. <a name="index-g_t_002di-259"></a><a name="index-g_t_002d_002d_002dcase_002dinsensitive-260"></a><a name="index-case_002dinsensitive-261"></a><dt>`<samp><span class="samp">-i, --case-insensitive, </span><code>%option case-insensitive</code></samp>'<dd>instructs <code>flex</code> to generate a <dfn>case-insensitive</dfn> scanner.  The
  2611. case of letters given in the <code>flex</code> input patterns will be ignored,
  2612. and tokens in the input will be matched regardless of case.  The matched
  2613. text given in <code>yytext</code> will have the preserved case (i.e., it will
  2614. not be folded).  For tricky behavior, see <a href="#case-and-character-ranges">case and character ranges</a>.
  2615.  
  2616.      <p><a name="option_002dlex_002dcompat"></a>
  2617. <a name="index-g_t_002dl-262"></a><a name="index-g_t_002d_002d_002dlex_002dcompat-263"></a><a name="index-lex_002dcompat-264"></a><br><dt>`<samp><span class="samp">-l, --lex-compat, </span><code>%option lex-compat</code></samp>'<dd>turns on maximum compatibility with the original AT&T <code>lex</code>
  2618. implementation.  Note that this does not mean <em>full</em> compatibility. 
  2619. Use of this option costs a considerable amount of performance, and it
  2620. cannot be used with the `<samp><span class="samp">--c++</span></samp>', `<samp><span class="samp">--full</span></samp>', `<samp><span class="samp">--fast</span></samp>', `<samp><span class="samp">-Cf</span></samp>', or
  2621. `<samp><span class="samp">-CF</span></samp>' options.  For details on the compatibilities it provides, see
  2622. <a href="#Lex-and-Posix">Lex and Posix</a>.  This option also results in the name
  2623. <code>YY_FLEX_LEX_COMPAT</code> being <code>#define</code>'d in the generated scanner.
  2624.  
  2625.      <p><a name="option_002dbatch"></a>
  2626. <a name="index-g_t_002dB-265"></a><a name="index-g_t_002d_002d_002dbatch-266"></a><a name="index-batch-267"></a><br><dt>`<samp><span class="samp">-B, --batch, </span><code>%option batch</code></samp>'<dd>instructs <code>flex</code> to generate a <dfn>batch</dfn> scanner, the opposite of
  2627. <em>interactive</em> scanners generated by `<samp><span class="samp">--interactive</span></samp>' (see below).  In
  2628. general, you use `<samp><span class="samp">-B</span></samp>' when you are <em>certain</em> that your scanner
  2629. will never be used interactively, and you want to squeeze a
  2630. <em>little</em> more performance out of it.  If your goal is instead to
  2631. squeeze out a <em>lot</em> more performance, you should be using the
  2632. `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' options, which turn on `<samp><span class="samp">--batch</span></samp>' automatically
  2633. anyway.
  2634.  
  2635.      <p><a name="option_002dinteractive"></a>
  2636. <a name="index-g_t_002dI-268"></a><a name="index-g_t_002d_002d_002dinteractive-269"></a><a name="index-interactive-270"></a><br><dt>`<samp><span class="samp">-I, --interactive, </span><code>%option interactive</code></samp>'<dd>instructs <code>flex</code> to generate an <i>interactive</i> scanner.  An
  2637. interactive scanner is one that only looks ahead to decide what token
  2638. has been matched if it absolutely must.  It turns out that always
  2639. looking one extra character ahead, even if the scanner has already seen
  2640. enough text to disambiguate the current token, is a bit faster than only
  2641. looking ahead when necessary.  But scanners that always look ahead give
  2642. dreadful interactive performance; for example, when a user types a
  2643. newline, it is not recognized as a newline token until they enter
  2644. <em>another</em> token, which often means typing in another whole line.
  2645.  
  2646.      <p><code>flex</code> scanners default to <code>interactive</code> unless you use the
  2647. `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' table-compression options
  2648. (see <a href="#Performance">Performance</a>).  That's because if you're looking for
  2649. high-performance you should be using one of these options, so if you
  2650. didn't, <code>flex</code> assumes you'd rather trade off a bit of run-time
  2651. performance for intuitive interactive behavior.  Note also that you
  2652. <em>cannot</em> use `<samp><span class="samp">--interactive</span></samp>' in conjunction with `<samp><span class="samp">-Cf</span></samp>' or
  2653. `<samp><span class="samp">-CF</span></samp>'.  Thus, this option is not really needed; it is on by default
  2654. for all those cases in which it is allowed.
  2655.  
  2656.      <p>You can force a scanner to
  2657. <em>not</em>
  2658. be interactive by using
  2659. `<samp><span class="samp">--batch</span></samp>'
  2660.  
  2661.      <p><a name="option_002d7bit"></a>
  2662. <a name="index-g_t_002d7-271"></a><a name="index-g_t_002d_002d_002d7bit-272"></a><a name="index-g_t7bit-273"></a><br><dt>`<samp><span class="samp">-7, --7bit, </span><code>%option 7bit</code></samp>'<dd>instructs <code>flex</code> to generate a 7-bit scanner, i.e., one which can
  2663. only recognize 7-bit characters in its input.  The advantage of using
  2664. `<samp><span class="samp">--7bit</span></samp>' is that the scanner's tables can be up to half the size of
  2665. those generated using the `<samp><span class="samp">--8bit</span></samp>'.  The disadvantage is that such
  2666. scanners often hang or crash if their input contains an 8-bit character.
  2667.  
  2668.      <p>Note, however, that unless you generate your scanner using the
  2669. `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' table compression options, use of `<samp><span class="samp">--7bit</span></samp>'
  2670. will save only a small amount of table space, and make your scanner
  2671. considerably less portable.  <code>Flex</code>'s default behavior is to
  2672. generate an 8-bit scanner unless you use the `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>',
  2673. in which case <code>flex</code> defaults to generating 7-bit scanners unless
  2674. your site was always configured to generate 8-bit scanners (as will
  2675. often be the case with non-USA sites).  You can tell whether flex
  2676. generated a 7-bit or an 8-bit scanner by inspecting the flag summary in
  2677. the `<samp><span class="samp">--verbose</span></samp>' output as described above.
  2678.  
  2679.      <p>Note that if you use `<samp><span class="samp">-Cfe</span></samp>' or `<samp><span class="samp">-CFe</span></samp>' <code>flex</code> still
  2680. defaults to generating an 8-bit scanner, since usually with these
  2681. compression options full 8-bit tables are not much more expensive than
  2682. 7-bit tables.
  2683.  
  2684.      <p><a name="option_002d8bit"></a>
  2685. <a name="index-g_t_002d8-274"></a><a name="index-g_t_002d_002d_002d8bit-275"></a><a name="index-g_t8bit-276"></a><br><dt>`<samp><span class="samp">-8, --8bit, </span><code>%option 8bit</code></samp>'<dd>instructs <code>flex</code> to generate an 8-bit scanner, i.e., one which can
  2686. recognize 8-bit characters.  This flag is only needed for scanners
  2687. generated using `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>', as otherwise flex defaults to
  2688. generating an 8-bit scanner anyway.
  2689.  
  2690.      <p>See the discussion of
  2691. `<samp><span class="samp">--7bit</span></samp>'
  2692. above for <code>flex</code>'s default behavior and the tradeoffs between 7-bit
  2693. and 8-bit scanners.
  2694.  
  2695.      <p><a name="option_002ddefault"></a>
  2696. <a name="index-g_t_002d_002d_002ddefault-277"></a><a name="index-default-278"></a><br><dt>`<samp><span class="samp">--default, </span><code>%option default</code></samp>'<dd>generate the default rule.
  2697.  
  2698.      <p><a name="option_002dalways_002dinteractive"></a>
  2699. <a name="index-g_t_002d_002d_002dalways_002dinteractive-279"></a><a name="index-always_002dinteractive-280"></a><br><dt>`<samp><span class="samp">--always-interactive, </span><code>%option always-interactive</code></samp>'<dd>instructs flex to generate a scanner which always considers its input
  2700. <em>interactive</em>.  Normally, on each new input file the scanner calls
  2701. <code>isatty()</code> in an attempt to determine whether the scanner's input
  2702. source is interactive and thus should be read a character at a time. 
  2703. When this option is used, however, then no such call is made.
  2704.  
  2705.      <p><a name="index-g_t_002d_002d_002dnever_002dinteractive-281"></a><br><dt>`<samp><span class="samp">--never-interactive, </span><code>--never-interactive</code></samp>'<dd>instructs flex to generate a scanner which never considers its input
  2706. interactive.  This is the opposite of <code>always-interactive</code>.
  2707.  
  2708.      <p><a name="option_002dposix"></a>
  2709. <a name="index-g_t_002dX-282"></a><a name="index-g_t_002d_002d_002dposix-283"></a><a name="index-posix-284"></a><br><dt>`<samp><span class="samp">-X, --posix, </span><code>%option posix</code></samp>'<dd>turns on maximum compatibility with the POSIX 1003.2-1992 definition of
  2710. <code>lex</code>.  Since <code>flex</code> was originally designed to implement the
  2711. POSIX definition of <code>lex</code> this generally involves very few changes
  2712. in behavior.  At the current writing the known differences between
  2713. <code>flex</code> and the POSIX standard are:
  2714.  
  2715.           <ul>
  2716. <li>In POSIX and AT&T <code>lex</code>, the repeat operator, `<samp><span class="samp">{}</span></samp>', has lower
  2717. precedence than concatenation (thus `<samp><span class="samp">ab{3}</span></samp>' yields `<samp><span class="samp">ababab</span></samp>'). 
  2718. Most POSIX utilities use an Extended Regular Expression (ERE) precedence
  2719. that has the precedence of the repeat operator higher than concatenation
  2720. (which causes `<samp><span class="samp">ab{3}</span></samp>' to yield `<samp><span class="samp">abbb</span></samp>').  By default, <code>flex</code>
  2721. places the precedence of the repeat operator higher than concatenation
  2722. which matches the ERE processing of other POSIX utilities.  When either
  2723. `<samp><span class="samp">--posix</span></samp>' or `<samp><span class="samp">-l</span></samp>' are specified, <code>flex</code> will use the
  2724. traditional AT&T and POSIX-compliant precedence for the repeat operator
  2725. where concatenation has higher precedence than the repeat operator. 
  2726. </ul>
  2727.  
  2728.      <p><a name="option_002dstack"></a>
  2729. <a name="index-g_t_002d_002d_002dstack-285"></a><a name="index-stack-286"></a><br><dt>`<samp><span class="samp">--stack, </span><code>%option stack</code></samp>'<dd>enables the use of
  2730. start condition stacks (see <a href="#Start-Conditions">Start Conditions</a>).
  2731.  
  2732.      <p><a name="option_002dstdinit"></a>
  2733. <a name="index-g_t_002d_002d_002dstdinit-287"></a><a name="index-stdinit-288"></a><br><dt>`<samp><span class="samp">--stdinit, </span><code>%option stdinit</code></samp>'<dd>if set (i.e., <b>%option stdinit)</b> initializes <code>yyin</code> and
  2734. <code>yyout</code> to <samp><span class="file">stdin</span></samp> and <samp><span class="file">stdout</span></samp>, instead of the default of
  2735. <samp><span class="file">NULL</span></samp>.  Some existing <code>lex</code> programs depend on this behavior,
  2736. even though it is not compliant with ANSI C, which does not require
  2737. <samp><span class="file">stdin</span></samp> and <samp><span class="file">stdout</span></samp> to be compile-time constant. In a
  2738. reentrant scanner, however, this is not a problem since initialization
  2739. is performed in <code>yylex_init</code> at runtime.
  2740.  
  2741.      <p><a name="option_002dyylineno"></a>
  2742. <a name="index-g_t_002d_002d_002dyylineno-289"></a><a name="index-yylineno-290"></a><br><dt>`<samp><span class="samp">--yylineno, </span><code>%option yylineno</code></samp>'<dd>directs <code>flex</code> to generate a scanner
  2743. that maintains the number of the current line read from its input in the
  2744. global variable <code>yylineno</code>.  This option is implied by <code>%option
  2745. lex-compat</code>.  In a reentrant C scanner, the macro <code>yylineno</code> is
  2746. accessible regardless of the value of <code>%option yylineno</code>, however, its
  2747. value is not modified by <code>flex</code> unless <code>%option yylineno</code> is enabled.
  2748.  
  2749.      <p><a name="option_002dyywrap"></a>
  2750. <a name="index-g_t_002d_002d_002dyywrap-291"></a><a name="index-yywrap-292"></a><br><dt>`<samp><span class="samp">--yywrap, </span><code>%option yywrap</code></samp>'<dd>if unset (i.e., <code>--noyywrap)</code>, makes the scanner not call
  2751. <code>yywrap()</code> upon an end-of-file, but simply assume that there are no
  2752. more files to scan (until the user points <samp><span class="file">yyin</span></samp> at a new file and
  2753. calls <code>yylex()</code> again).
  2754.  
  2755. </dl>
  2756.  
  2757. <div class="node">
  2758. <p><hr>
  2759. <a name="Code-Level-And-API-Options"></a>
  2760. <a name="Code_002dLevel-And-API-Options"></a>
  2761. Next: <a rel="next" accesskey="n" href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a>,
  2762. Previous: <a rel="previous" accesskey="p" href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a>,
  2763. Up: <a rel="up" accesskey="u" href="#Scanner-Options">Scanner Options</a>
  2764.  
  2765. </div>
  2766.  
  2767. <h3 class="section">16.3 Code-Level And API Options</h3>
  2768.  
  2769.      <dl>
  2770. <a name="option_002dansi_002ddefinitions"></a>
  2771. <a name="index-g_t_002d_002d_002doption_002dansi_002ddefinitions-293"></a><a name="index-ansi_002ddefinitions-294"></a><dt>`<samp><span class="samp">--ansi-definitions, </span><code>%option ansi-definitions</code></samp>'<dd>instruct flex to generate ANSI C99 definitions for functions. 
  2772. This option is enabled by default. 
  2773. If <code>%option noansi-definitions</code> is specified, then the obsolete style
  2774. is generated.
  2775.  
  2776.      <p><a name="option_002dansi_002dprototypes"></a>
  2777. <a name="index-g_t_002d_002d_002doption_002dansi_002dprototypes-295"></a><a name="index-ansi_002dprototypes-296"></a><br><dt>`<samp><span class="samp">--ansi-prototypes, </span><code>%option ansi-prototypes</code></samp>'<dd>instructs flex to generate ANSI C99 prototypes for functions. 
  2778. This option is enabled by default. 
  2779. If <code>noansi-prototypes</code> is specified, then
  2780. prototypes will have empty parameter lists.
  2781.  
  2782.      <p><a name="option_002dbison_002dbridge"></a>
  2783. <a name="index-g_t_002d_002d_002dbison_002dbridge-297"></a><a name="index-bison_002dbridge-298"></a><br><dt>`<samp><span class="samp">--bison-bridge, </span><code>%option bison-bridge</code></samp>'<dd>instructs flex to generate a C scanner that is
  2784. meant to be called by a
  2785. <code>GNU bison</code>
  2786. parser. The scanner has minor API changes for
  2787. <code>bison</code>
  2788. compatibility. In particular, the declaration of
  2789. <code>yylex</code>
  2790. is modified to take an additional parameter,
  2791. <code>yylval</code>. 
  2792. See <a href="#Bison-Bridge">Bison Bridge</a>.
  2793.  
  2794.      <p><a name="option_002dbison_002dlocations"></a>
  2795. <a name="index-g_t_002d_002d_002dbison_002dlocations-299"></a><a name="index-bison_002dlocations-300"></a><br><dt>`<samp><span class="samp">--bison-locations, </span><code>%option bison-locations</code></samp>'<dd>instruct flex that
  2796. <code>GNU bison</code> <code>%locations</code> are being used. 
  2797. This means <code>yylex</code> will be passed
  2798. an additional parameter, <code>yylloc</code>. This option
  2799. implies <code>%option bison-bridge</code>. 
  2800. See <a href="#Bison-Bridge">Bison Bridge</a>.
  2801.  
  2802.      <p><a name="option_002dnoline"></a>
  2803. <a name="index-g_t_002dL-301"></a><a name="index-g_t_002d_002d_002dnoline-302"></a><a name="index-noline-303"></a><br><dt>`<samp><span class="samp">-L, --noline, </span><code>%option noline</code></samp>'<dd>instructs
  2804. <code>flex</code>
  2805. not to generate
  2806. <code>#line</code>
  2807. directives.  Without this option,
  2808. <code>flex</code>
  2809. peppers the generated scanner
  2810. with <code>#line</code> directives so error messages in the actions will be correctly
  2811. located with respect to either the original
  2812. <code>flex</code>
  2813. input file (if the errors are due to code in the input file), or
  2814. <samp><span class="file">lex.yy.c</span></samp>
  2815. (if the errors are
  2816. <code>flex</code>'s
  2817. fault – you should report these sorts of errors to the email address
  2818. given in <a href="#Reporting-Bugs">Reporting Bugs</a>).
  2819.  
  2820.      <p><a name="option_002dreentrant"></a>
  2821. <a name="index-g_t_002dR-304"></a><a name="index-g_t_002d_002d_002dreentrant-305"></a><a name="index-reentrant-306"></a><br><dt>`<samp><span class="samp">-R, --reentrant, </span><code>%option reentrant</code></samp>'<dd>instructs flex to generate a reentrant C scanner.  The generated scanner
  2822. may safely be used in a multi-threaded environment. The API for a
  2823. reentrant scanner is different than for a non-reentrant scanner
  2824. see <a href="#Reentrant">Reentrant</a>).  Because of the API difference between
  2825. reentrant and non-reentrant <code>flex</code> scanners, non-reentrant flex
  2826. code must be modified before it is suitable for use with this option. 
  2827. This option is not compatible with the `<samp><span class="samp">--c++</span></samp>' option.
  2828.  
  2829.      <p>The option `<samp><span class="samp">--reentrant</span></samp>' does not affect the performance of
  2830. the scanner.
  2831.  
  2832.      <p><a name="option_002dc_002b_002b"></a>
  2833. <a name="index-g_t_002d_002b-307"></a><a name="index-g_t_002d_002d_002dc_002b_002b-308"></a><a name="index-c_002b_002b-309"></a><br><dt>`<samp><span class="samp">-+, --c++, </span><code>%option c++</code></samp>'<dd>specifies that you want flex to generate a C++
  2834. scanner class.  See <a href="#Cxx">Cxx</a>, for
  2835. details.
  2836.  
  2837.      <p><a name="option_002darray"></a>
  2838. <a name="index-g_t_002d_002d_002darray-310"></a><a name="index-array-311"></a><br><dt>`<samp><span class="samp">--array, </span><code>%option array</code></samp>'<dd>specifies that you want yytext to be an array instead of a char*
  2839.  
  2840.      <p><a name="option_002dpointer"></a>
  2841. <a name="index-g_t_002d_002d_002dpointer-312"></a><a name="index-pointer-313"></a><br><dt>`<samp><span class="samp">--pointer, </span><code>%option pointer</code></samp>'<dd>specify that  <code>yytext</code> should be a <code>char *</code>, not an array. 
  2842. This default is <code>char *</code>.
  2843.  
  2844.      <p><a name="option_002dprefix"></a>
  2845. <a name="index-g_t_002dP-314"></a><a name="index-g_t_002d_002d_002dprefix-315"></a><a name="index-prefix-316"></a><br><dt>`<samp><span class="samp">-PPREFIX, --prefix=PREFIX, </span><code>%option prefix="PREFIX"</code></samp>'<dd>changes the default `<samp><span class="samp">yy</span></samp>' prefix used by <code>flex</code> for all
  2846. globally-visible variable and function names to instead be
  2847. `<samp><span class="samp">PREFIX</span></samp>'.  For example, `<samp><span class="samp">--prefix=foo</span></samp>' changes the name of
  2848. <code>yytext</code> to <code>footext</code>.  It also changes the name of the default
  2849. output file from <samp><span class="file">lex.yy.c</span></samp> to <samp><span class="file">lex.foo.c</span></samp>.  Here is a partial
  2850. list of the names affected:
  2851.  
  2852.      <pre class="example">     <pre class="verbatim">          
  2853.               yy_create_buffer
  2854.               yy_delete_buffer
  2855.               yy_flex_debug
  2856.               yy_init_buffer
  2857.               yy_flush_buffer
  2858.               yy_load_buffer_state
  2859.               yy_switch_to_buffer
  2860.               yyin
  2861.               yyleng
  2862.               yylex
  2863.               yylineno
  2864.               yyout
  2865.               yyrestart
  2866.               yytext
  2867.               yywrap
  2868.               yyalloc
  2869.               yyrealloc
  2870.               yyfree
  2871.      </pre>
  2872.      </pre>
  2873.      <p>(If you are using a C++ scanner, then only <code>yywrap</code> and
  2874. <code>yyFlexLexer</code> are affected.)  Within your scanner itself, you can
  2875. still refer to the global variables and functions using either version
  2876. of their name; but externally, they have the modified name.
  2877.  
  2878.      <p>This option lets you easily link together multiple
  2879. <code>flex</code>
  2880. programs into the same executable.  Note, though, that using this
  2881. option also renames
  2882. <code>yywrap()</code>,
  2883. so you now
  2884. <em>must</em>
  2885. either
  2886. provide your own (appropriately-named) version of the routine for your
  2887. scanner, or use
  2888. <code>%option noyywrap</code>,
  2889. as linking with
  2890. `<samp><span class="samp">-lfl</span></samp>'
  2891. no longer provides one for you by default.
  2892.  
  2893.      <p><a name="option_002dmain"></a>
  2894. <a name="index-g_t_002d_002d_002dmain-317"></a><a name="index-main-318"></a><br><dt>`<samp><span class="samp">--main, </span><code>%option main</code></samp>'<dd> directs flex to provide a default <code>main()</code> program for the
  2895. scanner, which simply calls <code>yylex()</code>.  This option implies
  2896. <code>noyywrap</code> (see below).
  2897.  
  2898.      <p><a name="option_002dnounistd"></a>
  2899. <a name="index-g_t_002d_002d_002dnounistd-319"></a><a name="index-nounistd-320"></a><br><dt>`<samp><span class="samp">--nounistd, </span><code>%option nounistd</code></samp>'<dd>suppresses inclusion of the non-ANSI header file <samp><span class="file">unistd.h</span></samp>. This option
  2900. is meant to target environments in which <samp><span class="file">unistd.h</span></samp> does not exist. Be aware
  2901. that certain options may cause flex to generate code that relies on functions
  2902. normally found in <samp><span class="file">unistd.h</span></samp>, (e.g. <code>isatty()</code>, <code>read()</code>.) 
  2903. If you wish to use these functions, you will have to inform your compiler where
  2904. to find them. 
  2905. See <a href="#option_002dalways_002dinteractive">option-always-interactive</a>. See <a href="#option_002dread">option-read</a>.
  2906.  
  2907.      <p><a name="option_002dyyclass"></a>
  2908. <a name="index-g_t_002d_002d_002dyyclass-321"></a><a name="index-yyclass-322"></a><br><dt>`<samp><span class="samp">--yyclass, </span><code>%option yyclass="NAME"</code></samp>'<dd>only applies when generating a C++ scanner (the `<samp><span class="samp">--c++</span></samp>' option).  It
  2909. informs <code>flex</code> that you have derived <code>foo</code> as a subclass of
  2910. <code>yyFlexLexer</code>, so <code>flex</code> will place your actions in the member
  2911. function <code>foo::yylex()</code> instead of <code>yyFlexLexer::yylex()</code>.  It
  2912. also generates a <code>yyFlexLexer::yylex()</code> member function that emits
  2913. a run-time error (by invoking <code>yyFlexLexer::LexerError())</code> if
  2914. called.  See <a href="#Cxx">Cxx</a>.
  2915.  
  2916. </dl>
  2917.  
  2918. <div class="node">
  2919. <p><hr>
  2920. <a name="Options-for-Scanner-Speed-and-Size"></a>
  2921. Next: <a rel="next" accesskey="n" href="#Debugging-Options">Debugging Options</a>,
  2922. Previous: <a rel="previous" accesskey="p" href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a>,
  2923. Up: <a rel="up" accesskey="u" href="#Scanner-Options">Scanner Options</a>
  2924.  
  2925. </div>
  2926.  
  2927. <h3 class="section">16.4 Options for Scanner Speed and Size</h3>
  2928.  
  2929.      <dl>
  2930. <dt>`<samp><span class="samp">-C[aefFmr]</span></samp>'<dd>controls the degree of table compression and, more generally, trade-offs
  2931. between small scanners and fast scanners.
  2932.  
  2933.           
  2934. <a name="index-g_t_002dC-323"></a>
  2935. <dl><dt>`<samp><span class="samp">-C</span></samp>'<dd>A lone `<samp><span class="samp">-C</span></samp>' specifies that the scanner tables should be compressed
  2936. but neither equivalence classes nor meta-equivalence classes should be
  2937. used.
  2938.  
  2939.           <p><a name="option_002dalign"></a>
  2940. <a name="index-g_t_002dCa-324"></a><a name="index-g_t_002d_002d_002dalign-325"></a><a name="index-align-326"></a><br><dt>`<samp><span class="samp">-Ca, --align, </span><code>%option align</code></samp>'<dd>(“align”) instructs flex to trade off larger tables in the
  2941. generated scanner for faster performance because the elements of
  2942. the tables are better aligned for memory access and computation.  On some
  2943. RISC architectures, fetching and manipulating longwords is more efficient
  2944. than with smaller-sized units such as shortwords.  This option can
  2945. quadruple the size of the tables used by your scanner.
  2946.  
  2947.           <p><a name="option_002decs"></a>
  2948. <a name="index-g_t_002dCe-327"></a><a name="index-g_t_002d_002d_002decs-328"></a><a name="index-ecs-329"></a><br><dt>`<samp><span class="samp">-Ce, --ecs, </span><code>%option ecs</code></samp>'<dd>directs <code>flex</code> to construct <dfn>equivalence classes</dfn>, i.e., sets
  2949. of characters which have identical lexical properties (for example, if
  2950. the only appearance of digits in the <code>flex</code> input is in the
  2951. character class “[0-9]” then the digits '0', '1', ..., '9' will all be
  2952. put in the same equivalence class).  Equivalence classes usually give
  2953. dramatic reductions in the final table/object file sizes (typically a
  2954. factor of 2-5) and are pretty cheap performance-wise (one array look-up
  2955. per character scanned).
  2956.  
  2957.           <p><a name="index-g_t_002dCf-330"></a><br><dt>`<samp><span class="samp">-Cf</span></samp>'<dd>specifies that the <dfn>full</dfn> scanner tables should be generated -
  2958. <code>flex</code> should not compress the tables by taking advantages of
  2959. similar transition functions for different states.
  2960.  
  2961.           <p><a name="index-g_t_002dCF-331"></a><br><dt>`<samp><span class="samp">-CF</span></samp>'<dd>specifies that the alternate fast scanner representation (described
  2962. above under the `<samp><span class="samp">--fast</span></samp>' flag) should be used.  This option cannot be
  2963. used with `<samp><span class="samp">--c++</span></samp>'.
  2964.  
  2965.           <p><a name="option_002dmeta_002decs"></a>
  2966. <a name="index-g_t_002dCm-332"></a><a name="index-g_t_002d_002d_002dmeta_002decs-333"></a><a name="index-meta_002decs-334"></a><br><dt>`<samp><span class="samp">-Cm, --meta-ecs, </span><code>%option meta-ecs</code></samp>'<dd>directs
  2967. <code>flex</code>
  2968. to construct
  2969. <dfn>meta-equivalence classes</dfn>,
  2970. which are sets of equivalence classes (or characters, if equivalence
  2971. classes are not being used) that are commonly used together.  Meta-equivalence
  2972. classes are often a big win when using compressed tables, but they
  2973. have a moderate performance impact (one or two <code>if</code> tests and one
  2974. array look-up per character scanned).
  2975.  
  2976.           <p><a name="option_002dread"></a>
  2977. <a name="index-g_t_002dCr-335"></a><a name="index-g_t_002d_002d_002dread-336"></a><a name="index-read-337"></a><br><dt>`<samp><span class="samp">-Cr, --read, </span><code>%option read</code></samp>'<dd>causes the generated scanner to <em>bypass</em> use of the standard I/O
  2978. library (<code>stdio</code>) for input.  Instead of calling <code>fread()</code> or
  2979. <code>getc()</code>, the scanner will use the <code>read()</code> system call,
  2980. resulting in a performance gain which varies from system to system, but
  2981. in general is probably negligible unless you are also using `<samp><span class="samp">-Cf</span></samp>'
  2982. or `<samp><span class="samp">-CF</span></samp>'.  Using `<samp><span class="samp">-Cr</span></samp>' can cause strange behavior if, for
  2983. example, you read from <samp><span class="file">yyin</span></samp> using <code>stdio</code> prior to calling
  2984. the scanner (because the scanner will miss whatever text your previous
  2985. reads left in the <code>stdio</code> input buffer).  `<samp><span class="samp">-Cr</span></samp>' has no effect
  2986. if you define <code>YY_INPUT()</code> (see <a href="#Generated-Scanner">Generated Scanner</a>). 
  2987. </dl>
  2988.  
  2989.      <p>The options `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' and `<samp><span class="samp">-Cm</span></samp>' do not make sense
  2990. together - there is no opportunity for meta-equivalence classes if the
  2991. table is not being compressed.  Otherwise the options may be freely
  2992. mixed, and are cumulative.
  2993.  
  2994.      <p>The default setting is `<samp><span class="samp">-Cem</span></samp>', which specifies that <code>flex</code>
  2995. should generate equivalence classes and meta-equivalence classes.  This
  2996. setting provides the highest degree of table compression.  You can trade
  2997. off faster-executing scanners at the cost of larger tables with the
  2998. following generally being true:
  2999.  
  3000.      <pre class="example">     <pre class="verbatim">          
  3001.               slowest & smallest
  3002.                     -Cem
  3003.                     -Cm
  3004.                     -Ce
  3005.                     -C
  3006.                     -C{f,F}e
  3007.                     -C{f,F}
  3008.                     -C{f,F}a
  3009.               fastest & largest
  3010.      </pre>
  3011.      </pre>
  3012.      <p>Note that scanners with the smallest tables are usually generated and
  3013. compiled the quickest, so during development you will usually want to
  3014. use the default, maximal compression.
  3015.  
  3016.      <p>`<samp><span class="samp">-Cfe</span></samp>' is often a good compromise between speed and size for
  3017. production scanners.
  3018.  
  3019.      <p><a name="option_002dfull"></a>
  3020. <a name="index-g_t_002df-338"></a><a name="index-g_t_002d_002d_002dfull-339"></a><a name="index-full-340"></a><br><dt>`<samp><span class="samp">-f, --full, </span><code>%option full</code></samp>'<dd>specifies
  3021. <dfn>fast scanner</dfn>. 
  3022. No table compression is done and <code>stdio</code> is bypassed. 
  3023. The result is large but fast.  This option is equivalent to
  3024. `<samp><span class="samp">--Cfr</span></samp>'
  3025.  
  3026.      <p><a name="option_002dfast"></a>
  3027. <a name="index-g_t_002dF-341"></a><a name="index-g_t_002d_002d_002dfast-342"></a><a name="index-fast-343"></a><br><dt>`<samp><span class="samp">-F, --fast, </span><code>%option fast</code></samp>'<dd>specifies that the <em>fast</em> scanner table representation should be
  3028. used (and <code>stdio</code> bypassed).  This representation is about as fast
  3029. as the full table representation `<samp><span class="samp">--full</span></samp>', and for some sets of
  3030. patterns will be considerably smaller (and for others, larger).  In
  3031. general, if the pattern set contains both <em>keywords</em> and a
  3032. catch-all, <em>identifier</em> rule, such as in the set:
  3033.  
  3034.      <pre class="example">     <pre class="verbatim">          
  3035.               "case"    return TOK_CASE;
  3036.               "switch"  return TOK_SWITCH;
  3037.               ...
  3038.               "default" return TOK_DEFAULT;
  3039.               [a-z]+    return TOK_ID;
  3040.      </pre>
  3041.      </pre>
  3042.      <p>then you're better off using the full table representation.  If only
  3043. the <em>identifier</em> rule is present and you then use a hash table or some such
  3044. to detect the keywords, you're better off using
  3045. `<samp><span class="samp">--fast</span></samp>'.
  3046.  
  3047.      <p>This option is equivalent to `<samp><span class="samp">-CFr</span></samp>' (see below).  It cannot be used
  3048. with `<samp><span class="samp">--c++</span></samp>'.
  3049.  
  3050. </dl>
  3051.  
  3052. <div class="node">
  3053. <p><hr>
  3054. <a name="Debugging-Options"></a>
  3055. Next: <a rel="next" accesskey="n" href="#Miscellaneous-Options">Miscellaneous Options</a>,
  3056. Previous: <a rel="previous" accesskey="p" href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a>,
  3057. Up: <a rel="up" accesskey="u" href="#Scanner-Options">Scanner Options</a>
  3058.  
  3059. </div>
  3060.  
  3061. <h3 class="section">16.5 Debugging Options</h3>
  3062.  
  3063.      <dl>
  3064. <a name="option_002dbackup"></a>
  3065. <a name="index-g_t_002db-344"></a><a name="index-g_t_002d_002d_002dbackup-345"></a><a name="index-backup-346"></a><dt>`<samp><span class="samp">-b, --backup, </span><code>%option backup</code></samp>'<dd>Generate backing-up information to <samp><span class="file">lex.backup</span></samp>.  This is a list of
  3066. scanner states which require backing up and the input characters on
  3067. which they do so.  By adding rules one can remove backing-up states.  If
  3068. <em>all</em> backing-up states are eliminated and `<samp><span class="samp">-Cf</span></samp>' or <code>-CF</code>
  3069. is used, the generated scanner will run faster (see the `<samp><span class="samp">--perf-report</span></samp>' flag). 
  3070. Only users who wish to squeeze every last cycle out of their scanners
  3071. need worry about this option.  (see <a href="#Performance">Performance</a>).
  3072.  
  3073.      <p><a name="option_002ddebug"></a>
  3074. <a name="index-g_t_002dd-347"></a><a name="index-g_t_002d_002d_002ddebug-348"></a><a name="index-debug-349"></a><br><dt>`<samp><span class="samp">-d, --debug, </span><code>%option debug</code></samp>'<dd>makes the generated scanner run in <dfn>debug</dfn> mode.  Whenever a pattern
  3075. is recognized and the global variable <code>yy_flex_debug</code> is non-zero
  3076. (which is the default), the scanner will write to <samp><span class="file">stderr</span></samp> a line
  3077. of the form:
  3078.  
  3079.      <pre class="example">     <pre class="verbatim">          
  3080.               -accepting rule at line 53 ("the matched text")
  3081.      </pre>
  3082.      </pre>
  3083.      <p>The line number refers to the location of the rule in the file defining
  3084. the scanner (i.e., the file that was fed to flex).  Messages are also
  3085. generated when the scanner backs up, accepts the default rule, reaches
  3086. the end of its input buffer (or encounters a NUL; at this point, the two
  3087. look the same as far as the scanner's concerned), or reaches an
  3088. end-of-file.
  3089.  
  3090.      <p><a name="option_002dperf_002dreport"></a>
  3091. <a name="index-g_t_002dp-350"></a><a name="index-g_t_002d_002d_002dperf_002dreport-351"></a><a name="index-perf_002dreport-352"></a><br><dt>`<samp><span class="samp">-p, --perf-report, </span><code>%option perf-report</code></samp>'<dd>generates a performance report to <samp><span class="file">stderr</span></samp>.  The report consists of
  3092. comments regarding features of the <code>flex</code> input file which will
  3093. cause a serious loss of performance in the resulting scanner.  If you
  3094. give the flag twice, you will also get comments regarding features that
  3095. lead to minor performance losses.
  3096.  
  3097.      <p>Note that the use of <code>REJECT</code>, and
  3098. variable trailing context (see <a href="#Limitations">Limitations</a>) entails a substantial
  3099. performance penalty; use of <code>yymore()</code>, the `<samp><span class="samp">^</span></samp>' operator, and
  3100. the `<samp><span class="samp">--interactive</span></samp>' flag entail minor performance penalties.
  3101.  
  3102.      <p><a name="option_002dnodefault"></a>
  3103. <a name="index-g_t_002ds-353"></a><a name="index-g_t_002d_002d_002dnodefault-354"></a><a name="index-nodefault-355"></a><br><dt>`<samp><span class="samp">-s, --nodefault, </span><code>%option nodefault</code></samp>'<dd>causes the <em>default rule</em> (that unmatched scanner input is echoed
  3104. to <samp><span class="file">stdout)</span></samp> to be suppressed.  If the scanner encounters input
  3105. that does not match any of its rules, it aborts with an error.  This
  3106. option is useful for finding holes in a scanner's rule set.
  3107.  
  3108.      <p><a name="option_002dtrace"></a>
  3109. <a name="index-g_t_002dT-356"></a><a name="index-g_t_002d_002d_002dtrace-357"></a><a name="index-trace-358"></a><br><dt>`<samp><span class="samp">-T, --trace, </span><code>%option trace</code></samp>'<dd>makes <code>flex</code> run in <dfn>trace</dfn> mode.  It will generate a lot of
  3110. messages to <samp><span class="file">stderr</span></samp> concerning the form of the input and the
  3111. resultant non-deterministic and deterministic finite automata.  This
  3112. option is mostly for use in maintaining <code>flex</code>.
  3113.  
  3114.      <p><a name="option_002dnowarn"></a>
  3115. <a name="index-g_t_002dw-359"></a><a name="index-g_t_002d_002d_002dnowarn-360"></a><a name="index-nowarn-361"></a><br><dt>`<samp><span class="samp">-w, --nowarn, </span><code>%option nowarn</code></samp>'<dd>suppresses warning messages.
  3116.  
  3117.      <p><a name="option_002dverbose"></a>
  3118. <a name="index-g_t_002dv-362"></a><a name="index-g_t_002d_002d_002dverbose-363"></a><a name="index-verbose-364"></a><br><dt>`<samp><span class="samp">-v, --verbose, </span><code>%option verbose</code></samp>'<dd>specifies that <code>flex</code> should write to <samp><span class="file">stderr</span></samp> a summary of
  3119. statistics regarding the scanner it generates.  Most of the statistics
  3120. are meaningless to the casual <code>flex</code> user, but the first line
  3121. identifies the version of <code>flex</code> (same as reported by `<samp><span class="samp">--version</span></samp>'),
  3122. and the next line the flags used when generating the scanner, including
  3123. those that are on by default.
  3124.  
  3125.      <p><a name="option_002dwarn"></a>
  3126. <a name="index-g_t_002d_002d_002dwarn-365"></a><a name="index-warn-366"></a><br><dt>`<samp><span class="samp">--warn, </span><code>%option warn</code></samp>'<dd>warn about certain things. In particular, if the default rule can be
  3127. matched but no defualt rule has been given, the flex will warn you. 
  3128. We recommend using this option always.
  3129.  
  3130. </dl>
  3131.  
  3132. <div class="node">
  3133. <p><hr>
  3134. <a name="Miscellaneous-Options"></a>
  3135. Previous: <a rel="previous" accesskey="p" href="#Debugging-Options">Debugging Options</a>,
  3136. Up: <a rel="up" accesskey="u" href="#Scanner-Options">Scanner Options</a>
  3137.  
  3138. </div>
  3139.  
  3140. <h3 class="section">16.6 Miscellaneous Options</h3>
  3141.  
  3142.      
  3143. <a name="index-g_t_002dc-367"></a>
  3144. <dl><dt>`<samp><span class="samp">-c</span></samp>'<dd>is a do-nothing option included for POSIX compliance.
  3145.  
  3146.      <p><a name="index-g_t_002dh-368"></a><a name="index-g_t_002d_002d_002dhelp-369"></a>generates
  3147. <br><dt>`<samp><span class="samp">-h, -?, --help</span></samp>'<dd>generates a “help” summary of <code>flex</code>'s options to <samp><span class="file">stdout</span></samp>
  3148. and then exits.
  3149.  
  3150.      <p><a name="index-g_t_002dn-370"></a><br><dt>`<samp><span class="samp">-n</span></samp>'<dd>is another do-nothing option included only for
  3151. POSIX compliance.
  3152.  
  3153.      <p><a name="index-g_t_002dV-371"></a><a name="index-g_t_002d_002d_002dversion-372"></a><br><dt>`<samp><span class="samp">-V, --version</span></samp>'<dd>prints the version number to <samp><span class="file">stdout</span></samp> and exits.
  3154.  
  3155. </dl>
  3156.  
  3157. <div class="node">
  3158. <p><hr>
  3159. <a name="Performance"></a>
  3160. Next: <a rel="next" accesskey="n" href="#Cxx">Cxx</a>,
  3161. Previous: <a rel="previous" accesskey="p" href="#Scanner-Options">Scanner Options</a>,
  3162. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  3163.  
  3164. </div>
  3165.  
  3166. <h2 class="chapter">17 Performance Considerations</h2>
  3167.  
  3168. <p><a name="index-performance_002c-considerations-373"></a>The main design goal of <code>flex</code> is that it generate high-performance
  3169. scanners.  It has been optimized for dealing well with large sets of
  3170. rules.  Aside from the effects on scanner speed of the table compression
  3171. `<samp><span class="samp">-C</span></samp>' options outlined above, there are a number of options/actions
  3172. which degrade performance.  These are, from most expensive to least:
  3173.  
  3174.    <p><a name="index-REJECT_002c-performance-costs-374"></a><a name="index-yylineno_002c-performance-costs-375"></a><a name="index-trailing-context_002c-performance-costs-376"></a>
  3175. <pre class="example"><pre class="verbatim">     
  3176.          REJECT
  3177.          arbitrary trailing context
  3178.      
  3179.          pattern sets that require backing up
  3180.          %option yylineno
  3181.          %array
  3182.      
  3183.          %option interactive
  3184.          %option always-interactive
  3185.      
  3186.          @samp{^} beginning-of-line operator
  3187.          yymore()
  3188. </pre>
  3189. </pre>
  3190.    <p>with the first two all being quite expensive and the last two being
  3191. quite cheap.  Note also that <code>unput()</code> is implemented as a routine
  3192. call that potentially does quite a bit of work, while <code>yyless()</code> is
  3193. a quite-cheap macro. So if you are just putting back some excess text
  3194. you scanned, use <code>ss()</code>.
  3195.  
  3196.    <p><code>REJECT</code> should be avoided at all costs when performance is
  3197. important.  It is a particularly expensive option.
  3198.  
  3199.    <p>There is one case when <code>%option yylineno</code> can be expensive. That is when
  3200. your patterns match long tokens that could <em>possibly</em> contain a newline
  3201. character. There is no performance penalty for rules that can not possibly
  3202. match newlines, since flex does not need to check them for newlines.  In
  3203. general, you should avoid rules such as <code>[^f]+</code>, which match very long
  3204. tokens, including newlines, and may possibly match your entire file! A better
  3205. approach is to separate <code>[^f]+</code> into two rules:
  3206.  
  3207. <pre class="example"><pre class="verbatim">     
  3208.      %option yylineno
  3209.      %%
  3210.          [^f\n]+
  3211.          \n+
  3212. </pre>
  3213. </pre>
  3214.    <p>The above scanner does not incur a performance penalty.
  3215.  
  3216.    <p><a name="index-patterns_002c-tuning-for-performance-377"></a><a name="index-performance_002c-backing-up-378"></a><a name="index-backing-up_002c-example-of-eliminating-379"></a>Getting rid of backing up is messy and often may be an enormous amount
  3217. of work for a complicated scanner.  In principal, one begins by using
  3218. the `<samp><span class="samp">-b</span></samp>' flag to generate a <samp><span class="file">lex.backup</span></samp> file.  For example,
  3219. on the input:
  3220.  
  3221.    <p><a name="index-backing-up_002c-eliminating-380"></a>
  3222. <pre class="example"><pre class="verbatim">     
  3223.          %%
  3224.          foo        return TOK_KEYWORD;
  3225.          foobar     return TOK_KEYWORD;
  3226. </pre>
  3227. </pre>
  3228.    <p>the file looks like:
  3229.  
  3230. <pre class="example"><pre class="verbatim">     
  3231.          State #6 is non-accepting -
  3232.           associated rule line numbers:
  3233.                 2       3
  3234.           out-transitions: [ o ]
  3235.           jam-transitions: EOF [ \001-n  p-\177 ]
  3236.      
  3237.          State #8 is non-accepting -
  3238.           associated rule line numbers:
  3239.                 3
  3240.           out-transitions: [ a ]
  3241.           jam-transitions: EOF [ \001-`  b-\177 ]
  3242.      
  3243.          State #9 is non-accepting -
  3244.           associated rule line numbers:
  3245.                 3
  3246.           out-transitions: [ r ]
  3247.           jam-transitions: EOF [ \001-q  s-\177 ]
  3248.      
  3249.          Compressed tables always back up.
  3250. </pre>
  3251. </pre>
  3252.    <p>The first few lines tell us that there's a scanner state in which it can
  3253. make a transition on an 'o' but not on any other character, and that in
  3254. that state the currently scanned text does not match any rule.  The
  3255. state occurs when trying to match the rules found at lines 2 and 3 in
  3256. the input file.  If the scanner is in that state and then reads
  3257. something other than an 'o', it will have to back up to find a rule
  3258. which is matched.  With a bit of headscratching one can see that this
  3259. must be the state it's in when it has seen `<samp><span class="samp">fo</span></samp>'.  When this has
  3260. happened, if anything other than another `<samp><span class="samp">o</span></samp>' is seen, the scanner
  3261. will have to back up to simply match the `<samp><span class="samp">f</span></samp>' (by the default rule).
  3262.  
  3263.    <p>The comment regarding State #8 indicates there's a problem when
  3264. `<samp><span class="samp">foob</span></samp>' has been scanned.  Indeed, on any character other than an
  3265. `<samp><span class="samp">a</span></samp>', the scanner will have to back up to accept "foo".  Similarly,
  3266. the comment for State #9 concerns when `<samp><span class="samp">fooba</span></samp>' has been scanned and
  3267. an `<samp><span class="samp">r</span></samp>' does not follow.
  3268.  
  3269.    <p>The final comment reminds us that there's no point going to all the
  3270. trouble of removing backing up from the rules unless we're using
  3271. `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>', since there's no performance gain doing so
  3272. with compressed scanners.
  3273.  
  3274.    <p><a name="index-error-rules_002c-to-eliminate-backing-up-381"></a>The way to remove the backing up is to add “error” rules:
  3275.  
  3276.    <p><a name="index-backing-up_002c-eliminating-by-adding-error-rules-382"></a>
  3277. <pre class="example"><pre class="verbatim">     
  3278.          %%
  3279.          foo         return TOK_KEYWORD;
  3280.          foobar      return TOK_KEYWORD;
  3281.      
  3282.          fooba       |
  3283.          foob        |
  3284.          fo          {
  3285.                      /* false alarm, not really a keyword */
  3286.                      return TOK_ID;
  3287.                      }
  3288. </pre>
  3289. </pre>
  3290.    <p>Eliminating backing up among a list of keywords can also be done using a
  3291. “catch-all” rule:
  3292.  
  3293.    <p><a name="index-backing-up_002c-eliminating-with-catch_002dall-rule-383"></a>
  3294. <pre class="example"><pre class="verbatim">     
  3295.          %%
  3296.          foo         return TOK_KEYWORD;
  3297.          foobar      return TOK_KEYWORD;
  3298.      
  3299.          [a-z]+      return TOK_ID;
  3300. </pre>
  3301. </pre>
  3302.    <p>This is usually the best solution when appropriate.
  3303.  
  3304.    <p>Backing up messages tend to cascade.  With a complicated set of rules
  3305. it's not uncommon to get hundreds of messages.  If one can decipher
  3306. them, though, it often only takes a dozen or so rules to eliminate the
  3307. backing up (though it's easy to make a mistake and have an error rule
  3308. accidentally match a valid token.  A possible future <code>flex</code> feature
  3309. will be to automatically add rules to eliminate backing up).
  3310.  
  3311.    <p>It's important to keep in mind that you gain the benefits of eliminating
  3312. backing up only if you eliminate <em>every</em> instance of backing up. 
  3313. Leaving just one means you gain nothing.
  3314.  
  3315.    <p><em>Variable</em> trailing context (where both the leading and trailing
  3316. parts do not have a fixed length) entails almost the same performance
  3317. loss as <code>REJECT</code> (i.e., substantial).  So when possible a rule
  3318. like:
  3319.  
  3320.    <p><a name="index-trailing-context_002c-variable-length-384"></a>
  3321. <pre class="example"><pre class="verbatim">     
  3322.          %%
  3323.          mouse|rat/(cat|dog)   run();
  3324. </pre>
  3325. </pre>
  3326.    <p>is better written:
  3327.  
  3328. <pre class="example"><pre class="verbatim">     
  3329.          %%
  3330.          mouse/cat|dog         run();
  3331.          rat/cat|dog           run();
  3332. </pre>
  3333. </pre>
  3334.    <p>or as
  3335.  
  3336. <pre class="example"><pre class="verbatim">     
  3337.          %%
  3338.          mouse|rat/cat         run();
  3339.          mouse|rat/dog         run();
  3340. </pre>
  3341. </pre>
  3342.    <p>Note that here the special '|' action does <em>not</em> provide any
  3343. savings, and can even make things worse (see <a href="#Limitations">Limitations</a>).
  3344.  
  3345.    <p>Another area where the user can increase a scanner's performance (and
  3346. one that's easier to implement) arises from the fact that the longer the
  3347. tokens matched, the faster the scanner will run.  This is because with
  3348. long tokens the processing of most input characters takes place in the
  3349. (short) inner scanning loop, and does not often have to go through the
  3350. additional work of setting up the scanning environment (e.g.,
  3351. <code>yytext</code>) for the action.  Recall the scanner for C comments:
  3352.  
  3353.    <p><a name="index-performance-optimization_002c-matching-longer-tokens-385"></a>
  3354. <pre class="example"><pre class="verbatim">     
  3355.          %x comment
  3356.          %%
  3357.                  int line_num = 1;
  3358.      
  3359.          "/*"         BEGIN(comment);
  3360.      
  3361.          <comment>[^*\n]*
  3362.          <comment>"*"+[^*/\n]*
  3363.          <comment>\n             ++line_num;
  3364.          <comment>"*"+"/"        BEGIN(INITIAL);
  3365. </pre>
  3366. </pre>
  3367.    <p>This could be sped up by writing it as:
  3368.  
  3369. <pre class="example"><pre class="verbatim">     
  3370.          %x comment
  3371.          %%
  3372.                  int line_num = 1;
  3373.      
  3374.          "/*"         BEGIN(comment);
  3375.      
  3376.          <comment>[^*\n]*
  3377.          <comment>[^*\n]*\n      ++line_num;
  3378.          <comment>"*"+[^*/\n]*
  3379.          <comment>"*"+[^*/\n]*\n ++line_num;
  3380.          <comment>"*"+"/"        BEGIN(INITIAL);
  3381. </pre>
  3382. </pre>
  3383.    <p>Now instead of each newline requiring the processing of another action,
  3384. recognizing the newlines is distributed over the other rules to keep the
  3385. matched text as long as possible.  Note that <em>adding</em> rules does
  3386. <em>not</em> slow down the scanner!  The speed of the scanner is
  3387. independent of the number of rules or (modulo the considerations given
  3388. at the beginning of this section) how complicated the rules are with
  3389. regard to operators such as `<samp><span class="samp">*</span></samp>' and `<samp><span class="samp">|</span></samp>'.
  3390.  
  3391.    <p><a name="index-keywords_002c-for-performance-386"></a><a name="index-performance_002c-using-keywords-387"></a>A final example in speeding up a scanner: suppose you want to scan
  3392. through a file containing identifiers and keywords, one per line
  3393. and with no other extraneous characters, and recognize all the
  3394. keywords.  A natural first approach is:
  3395.  
  3396.    <p><a name="index-performance-optimization_002c-recognizing-keywords-388"></a>
  3397. <pre class="example"><pre class="verbatim">     
  3398.          %%
  3399.          asm      |
  3400.          auto     |
  3401.          break    |
  3402.          ... etc ...
  3403.          volatile |
  3404.          while    /* it's a keyword */
  3405.      
  3406.          .|\n     /* it's not a keyword */
  3407. </pre>
  3408. </pre>
  3409.    <p>To eliminate the back-tracking, introduce a catch-all rule:
  3410.  
  3411. <pre class="example"><pre class="verbatim">     
  3412.          %%
  3413.          asm      |
  3414.          auto     |
  3415.          break    |
  3416.          ... etc ...
  3417.          volatile |
  3418.          while    /* it's a keyword */
  3419.      
  3420.          [a-z]+   |
  3421.          .|\n     /* it's not a keyword */
  3422. </pre>
  3423. </pre>
  3424.    <p>Now, if it's guaranteed that there's exactly one word per line, then we
  3425. can reduce the total number of matches by a half by merging in the
  3426. recognition of newlines with that of the other tokens:
  3427.  
  3428. <pre class="example"><pre class="verbatim">     
  3429.          %%
  3430.          asm\n    |
  3431.          auto\n   |
  3432.          break\n  |
  3433.          ... etc ...
  3434.          volatile\n |
  3435.          while\n  /* it's a keyword */
  3436.      
  3437.          [a-z]+\n |
  3438.          .|\n     /* it's not a keyword */
  3439. </pre>
  3440. </pre>
  3441.    <p>One has to be careful here, as we have now reintroduced backing up
  3442. into the scanner.  In particular, while
  3443. <em>we</em>
  3444. know that there will never be any characters in the input stream
  3445. other than letters or newlines,
  3446. <code>flex</code>
  3447. can't figure this out, and it will plan for possibly needing to back up
  3448. when it has scanned a token like `<samp><span class="samp">auto</span></samp>' and then the next character
  3449. is something other than a newline or a letter.  Previously it would
  3450. then just match the `<samp><span class="samp">auto</span></samp>' rule and be done, but now it has no `<samp><span class="samp">auto</span></samp>'
  3451. rule, only a `<samp><span class="samp">auto\n</span></samp>' rule.  To eliminate the possibility of backing up,
  3452. we could either duplicate all rules but without final newlines, or,
  3453. since we never expect to encounter such an input and therefore don't
  3454. how it's classified, we can introduce one more catch-all rule, this
  3455. one which doesn't include a newline:
  3456.  
  3457. <pre class="example"><pre class="verbatim">     
  3458.          %%
  3459.          asm\n    |
  3460.          auto\n   |
  3461.          break\n  |
  3462.          ... etc ...
  3463.          volatile\n |
  3464.          while\n  /* it's a keyword */
  3465.      
  3466.          [a-z]+\n |
  3467.          [a-z]+   |
  3468.          .|\n     /* it's not a keyword */
  3469. </pre>
  3470. </pre>
  3471.    <p>Compiled with `<samp><span class="samp">-Cf</span></samp>', this is about as fast as one can get a
  3472. <code>flex</code> scanner to go for this particular problem.
  3473.  
  3474.    <p>A final note: <code>flex</code> is slow when matching <code>NUL</code>s,
  3475. particularly when a token contains multiple <code>NUL</code>s.  It's best to
  3476. write rules which match <em>short</em> amounts of text if it's anticipated
  3477. that the text will often include <code>NUL</code>s.
  3478.  
  3479.    <p>Another final note regarding performance: as mentioned in
  3480. <a href="#Matching">Matching</a>, dynamically resizing <code>yytext</code> to accommodate huge
  3481. tokens is a slow process because it presently requires that the (huge)
  3482. token be rescanned from the beginning.  Thus if performance is vital,
  3483. you should attempt to match “large” quantities of text but not
  3484. “huge” quantities, where the cutoff between the two is at about 8K
  3485. characters per token.
  3486.  
  3487. <div class="node">
  3488. <p><hr>
  3489. <a name="Cxx"></a>
  3490. Next: <a rel="next" accesskey="n" href="#Reentrant">Reentrant</a>,
  3491. Previous: <a rel="previous" accesskey="p" href="#Performance">Performance</a>,
  3492. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  3493.  
  3494. </div>
  3495.  
  3496. <h2 class="chapter">18 Generating C++ Scanners</h2>
  3497.  
  3498. <p><a name="index-c_002b_002b_002c-experimental-form-of-scanner-class-389"></a><a name="index-experimental-form-of-c_002b_002b-scanner-class-390"></a><strong>IMPORTANT</strong>: the present form of the scanning class is <em>experimental</em>
  3499. and may change considerably between major releases.
  3500.  
  3501.    <p><a name="index-C_002b_002b-391"></a><a name="index-member-functions_002c-C_002b_002b-392"></a><a name="index-methods_002c-c_002b_002b-393"></a><code>flex</code> provides two different ways to generate scanners for use
  3502. with C++.  The first way is to simply compile a scanner generated by
  3503. <code>flex</code> using a C++ compiler instead of a C compiler.  You should
  3504. not encounter any compilation errors (see <a href="#Reporting-Bugs">Reporting Bugs</a>).  You can
  3505. then use C++ code in your rule actions instead of C code.  Note that the
  3506. default input source for your scanner remains <samp><span class="file">yyin</span></samp>, and default
  3507. echoing is still done to <samp><span class="file">yyout</span></samp>.  Both of these remain <code>FILE
  3508. *</code> variables and not C++ <em>streams</em>.
  3509.  
  3510.    <p>You can also use <code>flex</code> to generate a C++ scanner class, using the
  3511. `<samp><span class="samp">-+</span></samp>' option (or, equivalently, <code>%option c++)</code>, which is
  3512. automatically specified if the name of the <code>flex</code> executable ends
  3513. in a '+', such as <code>flex++</code>.  When using this option, <code>flex</code>
  3514. defaults to generating the scanner to the file <samp><span class="file">lex.yy.cc</span></samp> instead
  3515. of <samp><span class="file">lex.yy.c</span></samp>.  The generated scanner includes the header file
  3516. <samp><span class="file">FlexLexer.h</span></samp>, which defines the interface to two C++ classes.
  3517.  
  3518.    <p>The first class,
  3519. <code>FlexLexer</code>,
  3520. provides an abstract base class defining the general scanner class
  3521. interface.  It provides the following member functions:
  3522.  
  3523.      
  3524. <a name="index-YYText-_0028C_002b_002b-only_0029-394"></a>
  3525. <dl><dt><code>const char* YYText()</code><dd>returns the text of the most recently matched token, the equivalent of
  3526. <code>yytext</code>.
  3527.  
  3528.      <p><a name="index-YYLeng-_0028C_002b_002b-only_0029-395"></a><br><dt><code>int YYLeng()</code><dd>returns the length of the most recently matched token, the equivalent of
  3529. <code>yyleng</code>.
  3530.  
  3531.      <p><a name="index-lineno-_0028C_002b_002b-only_0029-396"></a><br><dt><code>int lineno() const</code><dd>returns the current input line number (see <code>%option yylineno)</code>, or
  3532. <code>1</code> if <code>%option yylineno</code> was not used.
  3533.  
  3534.      <p><a name="index-set_005fdebug-_0028C_002b_002b-only_0029-397"></a><br><dt><code>void set_debug( int flag )</code><dd>sets the debugging flag for the scanner, equivalent to assigning to
  3535. <code>yy_flex_debug</code> (see <a href="#Scanner-Options">Scanner Options</a>).  Note that you must build
  3536. the scannerusing <code>%option debug</code> to include debugging information
  3537. in it.
  3538.  
  3539.      <p><a name="index-debug-_0028C_002b_002b-only_0029-398"></a><br><dt><code>int debug() const</code><dd>returns the current setting of the debugging flag. 
  3540. </dl>
  3541.  
  3542.    <p>Also provided are member functions equivalent to
  3543. <code>yy_switch_to_buffer()</code>, <code>yy_create_buffer()</code> (though the
  3544. first argument is an <code>istream*</code> object pointer and not a
  3545. <code>FILE*)</code>, <code>yy_flush_buffer()</code>, <code>yy_delete_buffer()</code>, and
  3546. <code>yyrestart()</code> (again, the first argument is a <code>istream*</code>
  3547. object pointer).
  3548.  
  3549.    <p><a name="index-yyFlexLexer-_0028C_002b_002b-only_0029-399"></a><a name="index-FlexLexer-_0028C_002b_002b-only_0029-400"></a>The second class defined in <samp><span class="file">FlexLexer.h</span></samp> is <code>yyFlexLexer</code>,
  3550. which is derived from <code>FlexLexer</code>.  It defines the following
  3551. additional member functions:
  3552.  
  3553.      
  3554. <a name="index-yyFlexLexer-constructor-_0028C_002b_002b-only_0029-401"></a>
  3555. <dl><dt><code>yyFlexLexer( istream* arg_yyin = 0, ostream* arg_yyout = 0 )</code><dd>constructs a <code>yyFlexLexer</code> object using the given streams for input
  3556. and output.  If not specified, the streams default to <code>cin</code> and
  3557. <code>cout</code>, respectively.
  3558.  
  3559.      <p><a name="index-yylex-_0028C_002b_002b-version_0029-402"></a><br><dt><code>virtual int yylex()</code><dd>performs the same role is <code>yylex()</code> does for ordinary <code>flex</code>
  3560. scanners: it scans the input stream, consuming tokens, until a rule's
  3561. action returns a value.  If you derive a subclass <code>S</code> from
  3562. <code>yyFlexLexer</code> and want to access the member functions and variables
  3563. of <code>S</code> inside <code>yylex()</code>, then you need to use <code>%option
  3564. yyclass="S"</code> to inform <code>flex</code> that you will be using that subclass
  3565. instead of <code>yyFlexLexer</code>.  In this case, rather than generating
  3566. <code>yyFlexLexer::yylex()</code>, <code>flex</code> generates <code>S::yylex()</code>
  3567. (and also generates a dummy <code>yyFlexLexer::yylex()</code> that calls
  3568. <code>yyFlexLexer::LexerError()</code> if called).
  3569.  
  3570.      <p><a name="index-switch_005fstreams-_0028C_002b_002b-only_0029-403"></a><br><dt><code>virtual void switch_streams(istream* new_in = 0, ostream* new_out = 0)</code><dd>reassigns <code>yyin</code> to <code>new_in</code> (if non-null) and <code>yyout</code> to
  3571. <code>new_out</code> (if non-null), deleting the previous input buffer if
  3572. <code>yyin</code> is reassigned.
  3573.  
  3574.      <br><dt><code>int yylex( istream* new_in, ostream* new_out = 0 )</code><dd>first switches the input streams via <code>switch_streams( new_in,
  3575. new_out )</code> and then returns the value of <code>yylex()</code>. 
  3576. </dl>
  3577.  
  3578.    <p>In addition, <code>yyFlexLexer</code> defines the following protected virtual
  3579. functions which you can redefine in derived classes to tailor the
  3580. scanner:
  3581.  
  3582.      
  3583. <a name="index-LexerInput-_0028C_002b_002b-only_0029-404"></a>
  3584. <dl><dt><code>virtual int LexerInput( char* buf, int max_size )</code><dd>reads up to <code>max_size</code> characters into <code>buf</code> and returns the
  3585. number of characters read.  To indicate end-of-input, return 0
  3586. characters.  Note that <code>interactive</code> scanners (see the `<samp><span class="samp">-B</span></samp>'
  3587. and `<samp><span class="samp">-I</span></samp>' flags in <a href="#Scanner-Options">Scanner Options</a>) define the macro
  3588. <code>YY_INTERACTIVE</code>.  If you redefine <code>LexerInput()</code> and need to
  3589. take different actions depending on whether or not the scanner might be
  3590. scanning an interactive input source, you can test for the presence of
  3591. this name via <code>#ifdef</code> statements.
  3592.  
  3593.      <p><a name="index-LexerOutput-_0028C_002b_002b-only_0029-405"></a><br><dt><code>virtual void LexerOutput( const char* buf, int size )</code><dd>writes out <code>size</code> characters from the buffer <code>buf</code>, which, while
  3594. <code>NUL</code>-terminated, may also contain internal <code>NUL</code>s if the
  3595. scanner's rules can match text with <code>NUL</code>s in them.
  3596.  
  3597.      <p><a name="index-error-reporting_002c-in-C_002b_002b-406"></a><a name="index-LexerError-_0028C_002b_002b-only_0029-407"></a><br><dt><code>virtual void LexerError( const char* msg )</code><dd>reports a fatal error message.  The default version of this function
  3598. writes the message to the stream <code>cerr</code> and exits. 
  3599. </dl>
  3600.  
  3601.    <p>Note that a <code>yyFlexLexer</code> object contains its <em>entire</em>
  3602. scanning state.  Thus you can use such objects to create reentrant
  3603. scanners, but see also <a href="#Reentrant">Reentrant</a>.  You can instantiate multiple
  3604. instances of the same <code>yyFlexLexer</code> class, and you can also combine
  3605. multiple C++ scanner classes together in the same program using the
  3606. `<samp><span class="samp">-P</span></samp>' option discussed above.
  3607.  
  3608.    <p>Finally, note that the <code>%array</code> feature is not available to C++
  3609. scanner classes; you must use <code>%pointer</code> (the default).
  3610.  
  3611.    <p>Here is an example of a simple C++ scanner:
  3612.  
  3613.    <p><a name="index-C_002b_002b-scanners_002c-use-of-408"></a>
  3614. <pre class="example"><pre class="verbatim">     
  3615.              // An example of using the flex C++ scanner class.
  3616.      
  3617.          %{
  3618.          int mylineno = 0;
  3619.          %}
  3620.      
  3621.          string  \"[^\n"]+\"
  3622.      
  3623.          ws      [ \t]+
  3624.      
  3625.          alpha   [A-Za-z]
  3626.          dig     [0-9]
  3627.          name    ({alpha}|{dig}|\$)({alpha}|{dig}|[_.\-/$])*
  3628.          num1    [-+]?{dig}+\.?([eE][-+]?{dig}+)?
  3629.          num2    [-+]?{dig}*\.{dig}+([eE][-+]?{dig}+)?
  3630.          number  {num1}|{num2}
  3631.      
  3632.          %%
  3633.      
  3634.          {ws}    /* skip blanks and tabs */
  3635.      
  3636.          "/*"    {
  3637.                  int c;
  3638.      
  3639.                  while((c = yyinput()) != 0)
  3640.                      {
  3641.                      if(c == '\n')
  3642.                          ++mylineno;
  3643.      
  3644.                      else if(c == @samp{*})
  3645.                          {
  3646.                          if((c = yyinput()) == '/')
  3647.                              break;
  3648.                          else
  3649.                              unput(c);
  3650.                          }
  3651.                      }
  3652.                  }
  3653.      
  3654.          {number}  cout  "number "  YYText()  '\n';
  3655.      
  3656.          \n        mylineno++;
  3657.      
  3658.          {name}    cout  "name "  YYText()  '\n';
  3659.      
  3660.          {string}  cout  "string "  YYText()  '\n';
  3661.      
  3662.          %%
  3663.      
  3664.          int main( int /* argc */, char** /* argv */ )
  3665.              {
  3666.              @code{flex}Lexer* lexer = new yyFlexLexer;
  3667.              while(lexer->yylex() != 0)
  3668.                  ;
  3669.              return 0;
  3670.              }
  3671. </pre>
  3672. </pre>
  3673.    <p><a name="index-C_002b_002b_002c-multiple-different-scanners-409"></a>If you want to create multiple (different) lexer classes, you use the
  3674. `<samp><span class="samp">-P</span></samp>' flag (or the <code>prefix=</code> option) to rename each
  3675. <code>yyFlexLexer</code> to some other `<samp><span class="samp">xxFlexLexer</span></samp>'.  You then can
  3676. include <samp><span class="file">FlexLexer.h></span></samp> in your other sources once per lexer class,
  3677. first renaming <code>yyFlexLexer</code> as follows:
  3678.  
  3679.    <p><a name="index-include-files_002c-with-C_002b_002b-410"></a><a name="index-header-files_002c-with-C_002b_002b-411"></a><a name="index-C_002b_002b-scanners_002c-including-multiple-scanners-412"></a>
  3680. <pre class="example"><pre class="verbatim">     
  3681.          #undef yyFlexLexer
  3682.          #define yyFlexLexer xxFlexLexer
  3683.          #include <FflexLexer.h>
  3684.      
  3685.          #undef yyFlexLexer
  3686.          #define yyFlexLexer zzFlexLexer
  3687.          #include FlexLexer.h>
  3688. </pre>
  3689. </pre>
  3690.    <p>if, for example, you used <code>%option prefix="xx"</code> for one of your
  3691. scanners and <code>%option prefix="zz"</code> for the other.
  3692.  
  3693. <div class="node">
  3694. <p><hr>
  3695. <a name="Reentrant"></a>
  3696. Next: <a rel="next" accesskey="n" href="#Lex-and-Posix">Lex and Posix</a>,
  3697. Previous: <a rel="previous" accesskey="p" href="#Cxx">Cxx</a>,
  3698. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  3699.  
  3700. </div>
  3701.  
  3702. <h2 class="chapter">19 Reentrant C Scanners</h2>
  3703.  
  3704. <p><a name="index-reentrant_002c-explanation-413"></a><code>flex</code> has the ability to generate a reentrant C scanner. This is
  3705. accomplished by specifying <code>%option reentrant</code> (`<samp><span class="samp">-R</span></samp>') The generated
  3706. scanner is both portable, and safe to use in one or more separate threads of
  3707. control.  The most common use for reentrant scanners is from within
  3708. multi-threaded applications.  Any thread may create and execute a reentrant
  3709. <code>flex</code> scanner without the need for synchronization with other threads.
  3710.  
  3711. <ul class="menu">
  3712. <li><a accesskey="1" href="#Reentrant-Uses">Reentrant Uses</a>
  3713. <li><a accesskey="2" href="#Reentrant-Overview">Reentrant Overview</a>
  3714. <li><a accesskey="3" href="#Reentrant-Example">Reentrant Example</a>
  3715. <li><a accesskey="4" href="#Reentrant-Detail">Reentrant Detail</a>
  3716. <li><a accesskey="5" href="#Reentrant-Functions">Reentrant Functions</a>
  3717. </ul>
  3718.  
  3719. <div class="node">
  3720. <p><hr>
  3721. <a name="Reentrant-Uses"></a>
  3722. Next: <a rel="next" accesskey="n" href="#Reentrant-Overview">Reentrant Overview</a>,
  3723. Previous: <a rel="previous" accesskey="p" href="#Reentrant">Reentrant</a>,
  3724. Up: <a rel="up" accesskey="u" href="#Reentrant">Reentrant</a>
  3725.  
  3726. </div>
  3727.  
  3728. <h3 class="section">19.1 Uses for Reentrant Scanners</h3>
  3729.  
  3730. <p>However, there are other uses for a reentrant scanner.  For example, you
  3731. could scan two or more files simultaneously to implement a <code>diff</code> at
  3732. the token level (i.e., instead of at the character level):
  3733.  
  3734.    <p><a name="index-reentrant-scanners_002c-multiple-interleaved-scanners-414"></a>
  3735. <pre class="example"><pre class="verbatim">     
  3736.          /* Example of maintaining more than one active scanner. */
  3737.      
  3738.          do {
  3739.              int tok1, tok2;
  3740.      
  3741.              tok1 = yylex( scanner_1 );
  3742.              tok2 = yylex( scanner_2 );
  3743.      
  3744.              if( tok1 != tok2 )
  3745.                  printf("Files are different.");
  3746.      
  3747.         } while ( tok1 && tok2 );
  3748. </pre>
  3749. </pre>
  3750.    <p>Another use for a reentrant scanner is recursion. 
  3751. (Note that a recursive scanner can also be created using a non-reentrant scanner and
  3752. buffer states. See <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a>.)
  3753.  
  3754.    <p>The following crude scanner supports the `<samp><span class="samp">eval</span></samp>' command by invoking
  3755. another instance of itself.
  3756.  
  3757.    <p><a name="index-reentrant-scanners_002c-recursive-invocation-415"></a>
  3758. <pre class="example"><pre class="verbatim">     
  3759.          /* Example of recursive invocation. */
  3760.      
  3761.          %option reentrant
  3762.      
  3763.          %%
  3764.          "eval(".+")"  {
  3765.                            yyscan_t scanner;
  3766.                            YY_BUFFER_STATE buf;
  3767.      
  3768.                            yylex_init( &scanner );
  3769.                            yytext[yyleng-1] = ' ';
  3770.      
  3771.                            buf = yy_scan_string( yytext + 5, scanner );
  3772.                            yylex( scanner );
  3773.      
  3774.                            yy_delete_buffer(buf,scanner);
  3775.                            yylex_destroy( scanner );
  3776.                       }
  3777.          ...
  3778.          %%
  3779. </pre>
  3780. </pre>
  3781.    <div class="node">
  3782. <p><hr>
  3783. <a name="Reentrant-Overview"></a>
  3784. Next: <a rel="next" accesskey="n" href="#Reentrant-Example">Reentrant Example</a>,
  3785. Previous: <a rel="previous" accesskey="p" href="#Reentrant-Uses">Reentrant Uses</a>,
  3786. Up: <a rel="up" accesskey="u" href="#Reentrant">Reentrant</a>
  3787.  
  3788. </div>
  3789.  
  3790. <h3 class="section">19.2 An Overview of the Reentrant API</h3>
  3791.  
  3792. <p><a name="index-reentrant_002c-API-explanation-416"></a>The API for reentrant scanners is different than for non-reentrant
  3793. scanners. Here is a quick overview of the API:
  3794.  
  3795.      <ul>
  3796. <code>%option reentrant</code> must be specified.
  3797.  
  3798.      <li>All functions take one additional argument: <code>yyscanner</code>
  3799.  
  3800.      <li>All global variables are replaced by their macro equivalents. 
  3801. (We tell you this because it may be important to you during debugging.)
  3802.  
  3803.      <li><code>yylex_init</code> and <code>yylex_destroy</code> must be called before and
  3804. after <code>yylex</code>, respectively.
  3805.  
  3806.      <li>Accessor methods (get/set functions) provide access to common
  3807. <code>flex</code> variables.
  3808.  
  3809.      <li>User-specific data can be stored in <code>yyextra</code>. 
  3810. </ul>
  3811.  
  3812. <div class="node">
  3813. <p><hr>
  3814. <a name="Reentrant-Example"></a>
  3815. Next: <a rel="next" accesskey="n" href="#Reentrant-Detail">Reentrant Detail</a>,
  3816. Previous: <a rel="previous" accesskey="p" href="#Reentrant-Overview">Reentrant Overview</a>,
  3817. Up: <a rel="up" accesskey="u" href="#Reentrant">Reentrant</a>
  3818.  
  3819. </div>
  3820.  
  3821. <h3 class="section">19.3 Reentrant Example</h3>
  3822.  
  3823. <p>First, an example of a reentrant scanner:
  3824. <a name="index-reentrant_002c-example-of-417"></a>
  3825. <pre class="example"><pre class="verbatim">     
  3826.          /* This scanner prints "//" comments. */
  3827.          %option reentrant stack
  3828.          %x COMMENT
  3829.          %%
  3830.          "//"                 yy_push_state( COMMENT, yyscanner);
  3831.          .|\n
  3832.          <COMMENT>\n          yy_pop_state( yyscanner );
  3833.          <COMMENT>[^\n]+      fprintf( yyout, "%s\n", yytext);
  3834.          %%
  3835.          int main ( int argc, char * argv[] )
  3836.          {
  3837.              yyscan_t scanner;
  3838.      
  3839.              yylex_init ( &scanner );
  3840.              yylex ( scanner );
  3841.              yylex_destroy ( scanner );
  3842.          return 0;
  3843.         }
  3844. </pre>
  3845. </pre>
  3846.    <div class="node">
  3847. <p><hr>
  3848. <a name="Reentrant-Detail"></a>
  3849. Next: <a rel="next" accesskey="n" href="#Reentrant-Functions">Reentrant Functions</a>,
  3850. Previous: <a rel="previous" accesskey="p" href="#Reentrant-Example">Reentrant Example</a>,
  3851. Up: <a rel="up" accesskey="u" href="#Reentrant">Reentrant</a>
  3852.  
  3853. </div>
  3854.  
  3855. <h3 class="section">19.4 The Reentrant API in Detail</h3>
  3856.  
  3857. <p>Here are the things you need to do or know to use the reentrant C API of
  3858. <code>flex</code>.
  3859.  
  3860. <ul class="menu">
  3861. <li><a accesskey="1" href="#Specify-Reentrant">Specify Reentrant</a>
  3862. <li><a accesskey="2" href="#Extra-Reentrant-Argument">Extra Reentrant Argument</a>
  3863. <li><a accesskey="3" href="#Global-Replacement">Global Replacement</a>
  3864. <li><a accesskey="4" href="#Init-and-Destroy-Functions">Init and Destroy Functions</a>
  3865. <li><a accesskey="5" href="#Accessor-Methods">Accessor Methods</a>
  3866. <li><a accesskey="6" href="#Extra-Data">Extra Data</a>
  3867. <li><a accesskey="7" href="#About-yyscan_005ft">About yyscan_t</a>
  3868. </ul>
  3869.  
  3870. <div class="node">
  3871. <p><hr>
  3872. <a name="Specify-Reentrant"></a>
  3873. Next: <a rel="next" accesskey="n" href="#Extra-Reentrant-Argument">Extra Reentrant Argument</a>,
  3874. Previous: <a rel="previous" accesskey="p" href="#Reentrant-Detail">Reentrant Detail</a>,
  3875. Up: <a rel="up" accesskey="u" href="#Reentrant-Detail">Reentrant Detail</a>
  3876.  
  3877. </div>
  3878.  
  3879. <h4 class="subsection">19.4.1 Declaring a Scanner As Reentrant</h4>
  3880.  
  3881. <p>%option reentrant (–reentrant) must be specified.
  3882.  
  3883.    <p>Notice that <code>%option reentrant</code> is specified in the above example
  3884. (see <a href="#Reentrant-Example">Reentrant Example</a>. Had this option not been specified,
  3885. <code>flex</code> would have happily generated a non-reentrant scanner without
  3886. complaining. You may explicitly specify <code>%option noreentrant</code>, if
  3887. you do <em>not</em> want a reentrant scanner, although it is not
  3888. necessary. The default is to generate a non-reentrant scanner.
  3889.  
  3890. <div class="node">
  3891. <p><hr>
  3892. <a name="Extra-Reentrant-Argument"></a>
  3893. Next: <a rel="next" accesskey="n" href="#Global-Replacement">Global Replacement</a>,
  3894. Previous: <a rel="previous" accesskey="p" href="#Specify-Reentrant">Specify Reentrant</a>,
  3895. Up: <a rel="up" accesskey="u" href="#Reentrant-Detail">Reentrant Detail</a>
  3896.  
  3897. </div>
  3898.  
  3899. <h4 class="subsection">19.4.2 The Extra Argument</h4>
  3900.  
  3901. <p><a name="index-reentrant_002c-calling-functions-418"></a><a name="index-yyscanner-_0028reentrant-only_0029-419"></a>All functions take one additional argument: <code>yyscanner</code>.
  3902.  
  3903.    <p>Notice that the calls to <code>yy_push_state</code> and <code>yy_pop_state</code>
  3904. both have an argument, <code>yyscanner</code> , that is not present in a
  3905. non-reentrant scanner.  Here are the declarations of
  3906. <code>yy_push_state</code> and <code>yy_pop_state</code> in the generated scanner:
  3907.  
  3908. <pre class="example"><pre class="verbatim">     
  3909.          static void yy_push_state  ( int new_state , yyscan_t yyscanner ) ;
  3910.          static void yy_pop_state  ( yyscan_t yyscanner  ) ;
  3911. </pre>
  3912. </pre>
  3913.    <p>Notice that the argument <code>yyscanner</code> appears in the declaration of
  3914. both functions.  In fact, all <code>flex</code> functions in a reentrant
  3915. scanner have this additional argument.  It is always the last argument
  3916. in the argument list, it is always of type <code>yyscan_t</code> (which is
  3917. typedef'd to <code>void *</code>) and it is
  3918. always named <code>yyscanner</code>.  As you may have guessed,
  3919. <code>yyscanner</code> is a pointer to an opaque data structure encapsulating
  3920. the current state of the scanner.  For a list of function declarations,
  3921. see <a href="#Reentrant-Functions">Reentrant Functions</a>. Note that preprocessor macros, such as
  3922. <code>BEGIN</code>, <code>ECHO</code>, and <code>REJECT</code>, do not take this
  3923. additional argument.
  3924.  
  3925. <div class="node">
  3926. <p><hr>
  3927. <a name="Global-Replacement"></a>
  3928. Next: <a rel="next" accesskey="n" href="#Init-and-Destroy-Functions">Init and Destroy Functions</a>,
  3929. Previous: <a rel="previous" accesskey="p" href="#Extra-Reentrant-Argument">Extra Reentrant Argument</a>,
  3930. Up: <a rel="up" accesskey="u" href="#Reentrant-Detail">Reentrant Detail</a>
  3931.  
  3932. </div>
  3933.  
  3934. <h4 class="subsection">19.4.3 Global Variables Replaced By Macros</h4>
  3935.  
  3936. <p><a name="index-reentrant_002c-accessing-flex-variables-420"></a>All global variables in traditional flex have been replaced by macro equivalents.
  3937.  
  3938.    <p>Note that in the above example, <code>yyout</code> and <code>yytext</code> are
  3939. not plain variables. These are macros that will expand to their equivalent lvalue. 
  3940. All of the familiar <code>flex</code> globals have been replaced by their macro
  3941. equivalents. In particular, <code>yytext</code>, <code>yyleng</code>, <code>yylineno</code>,
  3942. <code>yyin</code>, <code>yyout</code>, <code>yyextra</code>, <code>yylval</code>, and <code>yylloc</code>
  3943. are macros. You may safely use these macros in actions as if they were plain
  3944. variables. We only tell you this so you don't expect to link to these variables
  3945. externally. Currently, each macro expands to a member of an internal struct, e.g.,
  3946.  
  3947. <pre class="example"><pre class="verbatim">     
  3948.      #define yytext (((struct yyguts_t*)yyscanner)->yytext_r)
  3949. </pre>
  3950. </pre>
  3951.    <p>One important thing to remember about
  3952. <code>yytext</code>
  3953. and friends is that
  3954. <code>yytext</code>
  3955. is not a global variable in a reentrant
  3956. scanner, you can not access it directly from outside an action or from
  3957. other functions. You must use an accessor method, e.g.,
  3958. <code>yyget_text</code>,
  3959. to accomplish this. (See below).
  3960.  
  3961. <div class="node">
  3962. <p><hr>
  3963. <a name="Init-and-Destroy-Functions"></a>
  3964. Next: <a rel="next" accesskey="n" href="#Accessor-Methods">Accessor Methods</a>,
  3965. Previous: <a rel="previous" accesskey="p" href="#Global-Replacement">Global Replacement</a>,
  3966. Up: <a rel="up" accesskey="u" href="#Reentrant-Detail">Reentrant Detail</a>
  3967.  
  3968. </div>
  3969.  
  3970. <h4 class="subsection">19.4.4 Init and Destroy Functions</h4>
  3971.  
  3972. <p><a name="index-memory_002c-considerations-for-reentrant-scanners-421"></a><a name="index-reentrant_002c-initialization-422"></a><a name="index-yylex_005finit-423"></a><a name="index-yylex_005fdestroy-424"></a>
  3973. <code>yylex_init</code> and <code>yylex_destroy</code> must be called before and
  3974. after <code>yylex</code>, respectively.
  3975.  
  3976. <pre class="example"><pre class="verbatim">     
  3977.          int yylex_init ( yyscan_t * ptr_yy_globals ) ;
  3978.          int yylex ( yyscan_t yyscanner ) ;
  3979.          int yylex_destroy ( yyscan_t yyscanner ) ;
  3980. </pre>
  3981. </pre>
  3982.    <p>The function <code>yylex_init</code> must be called before calling any other
  3983. function. The argument to <code>yylex_init</code> is the address of an
  3984. uninitialized pointer to be filled in by <code>flex</code>. The contents of
  3985. <code>ptr_yy_globals</code> need not be initialized, since <code>flex</code> will
  3986. overwrite it anyway. The value stored in <code>ptr_yy_globals</code> should
  3987. thereafter be passed to <code>yylex()</code> and <b>yylex_destroy()</b>.  Flex
  3988. does not save the argument passed to <code>yylex_init</code>, so it is safe to
  3989. pass the address of a local pointer to <code>yylex_init</code>.  The function
  3990. <code>yylex</code> should be familiar to you by now. The reentrant version
  3991. takes one argument, which is the value returned (via an argument) by
  3992. <code>yylex_init</code>.  Otherwise, it behaves the same as the non-reentrant
  3993. version of <code>yylex</code>.
  3994.  
  3995.    <p><code>yylex_init</code> returns 0 (zero) on success, or non-zero on failure,
  3996. in which case, errno is set to one of the following values:
  3997.  
  3998.      <ul>
  3999. <li>ENOMEM
  4000. Memory allocation error. See <a href="#memory_002dmanagement">memory-management</a>. 
  4001. <li>EINVAL
  4002. Invalid argument. 
  4003. </ul>
  4004.  
  4005.    <p>The function <code>yylex_destroy</code> should be
  4006. called to free resources used by the scanner. After <code>yylex_destroy</code>
  4007. is called, the contents of <code>yyscanner</code> should not be used.  Of
  4008. course, there is no need to destroy a scanner if you plan to reuse it. 
  4009. A <code>flex</code> scanner (both reentrant and non-reentrant) may be
  4010. restarted by calling <code>yyrestart</code>.
  4011.  
  4012.    <p>Below is an example of a program that creates a scanner, uses it, then destroys
  4013. it when done:
  4014.  
  4015. <pre class="example"><pre class="verbatim">     
  4016.          int main ()
  4017.          {
  4018.              yyscan_t scanner;
  4019.              int tok;
  4020.      
  4021.              yylex_init(&scanner);
  4022.      
  4023.              while ((tok=yylex()) > 0)
  4024.                  printf("tok=%d  yytext=%s\n", tok, yyget_text(scanner));
  4025.      
  4026.              yylex_destroy(scanner);
  4027.              return 0;
  4028.          }
  4029. </pre>
  4030. </pre>
  4031.    <div class="node">
  4032. <p><hr>
  4033. <a name="Accessor-Methods"></a>
  4034. Next: <a rel="next" accesskey="n" href="#Extra-Data">Extra Data</a>,
  4035. Previous: <a rel="previous" accesskey="p" href="#Init-and-Destroy-Functions">Init and Destroy Functions</a>,
  4036. Up: <a rel="up" accesskey="u" href="#Reentrant-Detail">Reentrant Detail</a>
  4037.  
  4038. </div>
  4039.  
  4040. <h4 class="subsection">19.4.5 Accessing Variables with Reentrant Scanners</h4>
  4041.  
  4042. <p><a name="index-reentrant_002c-accessor-functions-425"></a>Accessor methods (get/set functions) provide access to common
  4043. <code>flex</code> variables.
  4044.  
  4045.    <p>Many scanners that you build will be part of a larger project. Portions
  4046. of your project will need access to <code>flex</code> values, such as
  4047. <code>yytext</code>.  In a non-reentrant scanner, these values are global, so
  4048. there is no problem accessing them. However, in a reentrant scanner, there are no
  4049. global <code>flex</code> values. You can not access them directly.  Instead,
  4050. you must access <code>flex</code> values using accessor methods (get/set
  4051. functions). Each accessor method is named <code>yyget_NAME</code> or
  4052. <code>yyset_NAME</code>, where <code>NAME</code> is the name of the <code>flex</code>
  4053. variable you want. For example:
  4054.  
  4055.    <p><a name="index-accessor-functions_002c-use-of-426"></a>
  4056. <pre class="example"><pre class="verbatim">     
  4057.          /* Set the last character of yytext to NULL. */
  4058.          void chop ( yyscan_t scanner )
  4059.          {
  4060.              int len = yyget_leng( scanner );
  4061.              yyget_text( scanner )[len - 1] = '\0';
  4062.          }
  4063. </pre>
  4064. </pre>
  4065.    <p>The above code may be called from within an action like this:
  4066.  
  4067. <pre class="example"><pre class="verbatim">     
  4068.          %%
  4069.          .+\n    { chop( yyscanner );}
  4070. </pre>
  4071. </pre>
  4072.    <p>You may find that <code>%option header-file</code> is particularly useful for generating
  4073. prototypes of all the accessor functions. See <a href="#option_002dheader">option-header</a>.
  4074.  
  4075. <div class="node">
  4076. <p><hr>
  4077. <a name="Extra-Data"></a>
  4078. Next: <a rel="next" accesskey="n" href="#About-yyscan_005ft">About yyscan_t</a>,
  4079. Previous: <a rel="previous" accesskey="p" href="#Accessor-Methods">Accessor Methods</a>,
  4080. Up: <a rel="up" accesskey="u" href="#Reentrant-Detail">Reentrant Detail</a>
  4081.  
  4082. </div>
  4083.  
  4084. <h4 class="subsection">19.4.6 Extra Data</h4>
  4085.  
  4086. <p><a name="index-reentrant_002c-extra-data-427"></a><a name="index-yyextra-428"></a>User-specific data can be stored in <code>yyextra</code>.
  4087.  
  4088.    <p>In a reentrant scanner, it is unwise to use global variables to
  4089. communicate with or maintain state between different pieces of your program. 
  4090. However, you may need access to external data or invoke external functions
  4091. from within the scanner actions. 
  4092. Likewise, you may need to pass information to your scanner
  4093. (e.g., open file descriptors, or database connections). 
  4094. In a non-reentrant scanner, the only way to do this would be through the
  4095. use of global variables. 
  4096. <code>Flex</code> allows you to store arbitrary, “extra” data in a scanner. 
  4097. This data is accessible through the accessor methods
  4098. <code>yyget_extra</code>
  4099. and
  4100. <code>yyset_extra</code>
  4101. from outside the scanner, and through the shortcut macro
  4102. <code>yyextra</code>
  4103. from within the scanner itself. They are defined as follows:
  4104.  
  4105.    <p><a name="index-YY_005fEXTRA_005fTYPE-_0028reentrant-only_0029-429"></a><a name="index-yyget_005fextra-430"></a><a name="index-yyset_005fextra-431"></a>
  4106. <pre class="example"><pre class="verbatim">     
  4107.          #define YY_EXTRA_TYPE  void*
  4108.          YY_EXTRA_TYPE  yyget_extra ( yyscan_t scanner );
  4109.          void           yyset_extra ( YY_EXTRA_TYPE arbitrary_data , yyscan_t scanner);
  4110. </pre>
  4111. </pre>
  4112.    <p>By default, <code>YY_EXTRA_TYPE</code> is defined as type <code>void *</code>.  You
  4113. will have to cast <code>yyextra</code> and the return value from
  4114. <code>yyget_extra</code> to the appropriate value each time you access the
  4115. extra data.  To avoid casting, you may override the default type by
  4116. defining <code>YY_EXTRA_TYPE</code> in section 1 of your scanner:
  4117.  
  4118.    <p><a name="index-YY_005fEXTRA_005fTYPE_002c-defining-your-own-type-432"></a>
  4119. <pre class="example"><pre class="verbatim">     
  4120.          /* An example of overriding YY_EXTRA_TYPE. */
  4121.          %{
  4122.          #include <sys/stat.h>
  4123.          #include <unistd.h>
  4124.          #define YY_EXTRA_TYPE  struct stat*
  4125.          %}
  4126.          %option reentrant
  4127.          %%
  4128.      
  4129.          __filesize__     printf( "%ld", yyextra->st_size  );
  4130.          __lastmod__      printf( "%ld", yyextra->st_mtime );
  4131.          %%
  4132.          void scan_file( char* filename )
  4133.          {
  4134.              yyscan_t scanner;
  4135.              struct stat buf;
  4136.      
  4137.              yylex_init ( &scanner );
  4138.              yyset_in( fopen(filename,"r"), scanner );
  4139.      
  4140.              stat( filename, &buf);
  4141.              yyset_extra( &buf, scanner );
  4142.              yylex ( scanner );
  4143.              yylex_destroy( scanner );
  4144.         }
  4145. </pre>
  4146. </pre>
  4147.    <div class="node">
  4148. <p><hr>
  4149. <a name="About-yyscan_t"></a>
  4150. <a name="About-yyscan_005ft"></a>
  4151. Previous: <a rel="previous" accesskey="p" href="#Extra-Data">Extra Data</a>,
  4152. Up: <a rel="up" accesskey="u" href="#Reentrant-Detail">Reentrant Detail</a>
  4153.  
  4154. </div>
  4155.  
  4156. <h4 class="subsection">19.4.7 About yyscan_t</h4>
  4157.  
  4158. <p><a name="index-yyscan_005ft-_0028reentrant-only_0029-433"></a><code>yyscan_t</code> is defined as:
  4159.  
  4160. <pre class="example"><pre class="verbatim">     
  4161.           typedef void* yyscan_t;
  4162. </pre>
  4163. </pre>
  4164.    <p>It is initialized by <code>yylex_init()</code> to point to
  4165. an internal structure. You should never access this value
  4166. directly. In particular, you should never attempt to free it
  4167. (use <code>yylex_destroy()</code> instead.)
  4168.  
  4169. <div class="node">
  4170. <p><hr>
  4171. <a name="Reentrant-Functions"></a>
  4172. Previous: <a rel="previous" accesskey="p" href="#Reentrant-Detail">Reentrant Detail</a>,
  4173. Up: <a rel="up" accesskey="u" href="#Reentrant">Reentrant</a>
  4174.  
  4175. </div>
  4176.  
  4177. <h3 class="section">19.5 Functions and Macros Available in Reentrant C Scanners</h3>
  4178.  
  4179. <p>The following Functions are available in a reentrant scanner:
  4180.  
  4181.    <p><a name="index-yyget_005ftext-434"></a><a name="index-yyget_005fleng-435"></a><a name="index-yyget_005fin-436"></a><a name="index-yyget_005fout-437"></a><a name="index-yyget_005flineno-438"></a><a name="index-yyset_005fin-439"></a><a name="index-yyset_005fout-440"></a><a name="index-yyset_005flineno-441"></a><a name="index-yyget_005fdebug-442"></a><a name="index-yyset_005fdebug-443"></a><a name="index-yyget_005fextra-444"></a><a name="index-yyset_005fextra-445"></a>
  4182. <pre class="example"><pre class="verbatim">     
  4183.          char *yyget_text ( yyscan_t scanner );
  4184.          int yyget_leng ( yyscan_t scanner );
  4185.          FILE *yyget_in ( yyscan_t scanner );
  4186.          FILE *yyget_out ( yyscan_t scanner );
  4187.          int yyget_lineno ( yyscan_t scanner );
  4188.          YY_EXTRA_TYPE yyget_extra ( yyscan_t scanner );
  4189.          int  yyget_debug ( yyscan_t scanner );
  4190.      
  4191.          void yyset_debug ( int flag, yyscan_t scanner );
  4192.          void yyset_in  ( FILE * in_str , yyscan_t scanner );
  4193.          void yyset_out  ( FILE * out_str , yyscan_t scanner );
  4194.          void yyset_lineno ( int line_number , yyscan_t scanner );
  4195.          void yyset_extra ( YY_EXTRA_TYPE user_defined , yyscan_t scanner );
  4196. </pre>
  4197. </pre>
  4198.    <p>There are no “set” functions for yytext and yyleng. This is intentional.
  4199.  
  4200.    <p>The following Macro shortcuts are available in actions in a reentrant
  4201. scanner:
  4202.  
  4203. <pre class="example"><pre class="verbatim">     
  4204.          yytext
  4205.          yyleng
  4206.          yyin
  4207.          yyout
  4208.          yylineno
  4209.          yyextra
  4210.          yy_flex_debug
  4211. </pre>
  4212. </pre>
  4213.    <p><a name="index-yylineno_002c-in-a-reentrant-scanner-446"></a>In a reentrant C scanner, support for yylineno is always present
  4214. (i.e., you may access yylineno), but the value is never modified by
  4215. <code>flex</code> unless <code>%option yylineno</code> is enabled. This is to allow
  4216. the user to maintain the line count independently of <code>flex</code>.
  4217.  
  4218.    <p><a name="bison_002dfunctions"></a>
  4219. The following functions and macros are made available when <code>%option
  4220. bison-bridge</code> (`<samp><span class="samp">--bison-bridge</span></samp>') is specified:
  4221.  
  4222. <pre class="example"><pre class="verbatim">     
  4223.          YYSTYPE * yyget_lval ( yyscan_t scanner );
  4224.          void yyset_lval ( YYSTYPE * yylvalp , yyscan_t scanner );
  4225.          yylval
  4226. </pre>
  4227. </pre>
  4228.    <p>The following functions and macros are made available
  4229. when <code>%option bison-locations</code> (`<samp><span class="samp">--bison-locations</span></samp>') is specified:
  4230.  
  4231. <pre class="example"><pre class="verbatim">     
  4232.          YYLTYPE *yyget_lloc ( yyscan_t scanner );
  4233.          void yyset_lloc ( YYLTYPE * yyllocp , yyscan_t scanner );
  4234.          yylloc
  4235. </pre>
  4236. </pre>
  4237.    <p>Support for yylval assumes that <code>YYSTYPE</code> is a valid type.  Support for
  4238. yylloc assumes that <code>YYSLYPE</code> is a valid type.  Typically, these types are
  4239. generated by <code>bison</code>, and are included in section 1 of the <code>flex</code>
  4240. input.
  4241.  
  4242. <div class="node">
  4243. <p><hr>
  4244. <a name="Lex-and-Posix"></a>
  4245. Next: <a rel="next" accesskey="n" href="#Memory-Management">Memory Management</a>,
  4246. Previous: <a rel="previous" accesskey="p" href="#Reentrant">Reentrant</a>,
  4247. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  4248.  
  4249. </div>
  4250.  
  4251. <h2 class="chapter">20 Incompatibilities with Lex and Posix</h2>
  4252.  
  4253. <p><a name="index-POSIX-and-lex-447"></a><a name="index-lex-_0028traditional_0029-and-POSIX-448"></a>
  4254. <code>flex</code> is a rewrite of the AT&T Unix <em>lex</em> tool (the two
  4255. implementations do not share any code, though), with some extensions and
  4256. incompatibilities, both of which are of concern to those who wish to
  4257. write scanners acceptable to both implementations.  <code>flex</code> is fully
  4258. compliant with the POSIX <code>lex</code> specification, except that when
  4259. using <code>%pointer</code> (the default), a call to <code>unput()</code> destroys
  4260. the contents of <code>yytext</code>, which is counter to the POSIX
  4261. specification.  In this section we discuss all of the known areas of
  4262. incompatibility between <code>flex</code>, AT&T <code>lex</code>, and the POSIX
  4263. specification.  <code>flex</code>'s `<samp><span class="samp">-l</span></samp>' option turns on maximum
  4264. compatibility with the original AT&T <code>lex</code> implementation, at the
  4265. cost of a major loss in the generated scanner's performance.  We note
  4266. below which incompatibilities can be overcome using the `<samp><span class="samp">-l</span></samp>'
  4267. option.  <code>flex</code> is fully compatible with <code>lex</code> with the
  4268. following exceptions:
  4269.  
  4270.      <ul>
  4271. <li>The undocumented <code>lex</code> scanner internal variable <code>yylineno</code> is
  4272. not supported unless `<samp><span class="samp">-l</span></samp>' or <code>%option yylineno</code> is used.
  4273.  
  4274.      <li><code>yylineno</code> should be maintained on a per-buffer basis, rather than
  4275. a per-scanner (single global variable) basis.
  4276.  
  4277.      <li><code>yylineno</code> is not part of the POSIX specification.
  4278.  
  4279.      <li>The <code>input()</code> routine is not redefinable, though it may be called
  4280. to read characters following whatever has been matched by a rule.  If
  4281. <code>input()</code> encounters an end-of-file the normal <code>yywrap()</code>
  4282. processing is done.  A “real” end-of-file is returned by
  4283. <code>input()</code> as <code>EOF</code>.
  4284.  
  4285.      <li>Input is instead controlled by defining the <code>YY_INPUT()</code> macro.
  4286.  
  4287.      <li>The <code>flex</code> restriction that <code>input()</code> cannot be redefined is
  4288. in accordance with the POSIX specification, which simply does not
  4289. specify any way of controlling the scanner's input other than by making
  4290. an initial assignment to <samp><span class="file">yyin</span></samp>.
  4291.  
  4292.      <li>The <code>unput()</code> routine is not redefinable.  This restriction is in
  4293. accordance with POSIX.
  4294.  
  4295.      <li><code>flex</code> scanners are not as reentrant as <code>lex</code> scanners.  In
  4296. particular, if you have an interactive scanner and an interrupt handler
  4297. which long-jumps out of the scanner, and the scanner is subsequently
  4298. called again, you may get the following message:
  4299.  
  4300.      <p><a name="index-error-messages_002c-end-of-buffer-missed-449"></a>
  4301.      <pre class="example">     <pre class="verbatim">          
  4302.               fatal @code{flex} scanner internal error--end of buffer missed
  4303.      </pre>
  4304.      </pre>
  4305.      <p>To reenter the scanner, first use:
  4306.  
  4307.      <p><a name="index-restarting-the-scanner-450"></a>
  4308.      <pre class="example">     <pre class="verbatim">          
  4309.               yyrestart( yyin );
  4310.      </pre>
  4311.      </pre>
  4312.      <p>Note that this call will throw away any buffered input; usually this
  4313. isn't a problem with an interactive scanner. See <a href="#Reentrant">Reentrant</a>, for
  4314. <code>flex</code>'s reentrant API.
  4315.  
  4316.      <li>Also note that <code>flex</code> C++ scanner classes
  4317. <em>are</em>
  4318. reentrant, so if using C++ is an option for you, you should use
  4319. them instead.  See <a href="#Cxx">Cxx</a>, and <a href="#Reentrant">Reentrant</a>  for details.
  4320.  
  4321.      <li><code>output()</code> is not supported.  Output from the <b>ECHO</b> macro is
  4322. done to the file-pointer <code>yyout</code> (default <samp><span class="file">stdout)</span></samp>.
  4323.  
  4324.      <li><code>output()</code> is not part of the POSIX specification.
  4325.  
  4326.      <li><code>lex</code> does not support exclusive start conditions (%x), though they
  4327. are in the POSIX specification.
  4328.  
  4329.      <li>When definitions are expanded, <code>flex</code> encloses them in parentheses. 
  4330. With <code>lex</code>, the following:
  4331.  
  4332.      <p><a name="index-name-definitions_002c-not-POSIX-451"></a>
  4333.      <pre class="example">     <pre class="verbatim">          
  4334.               NAME    [A-Z][A-Z0-9]*
  4335.               %%
  4336.               foo{NAME}?      printf( "Found it\n" );
  4337.               %%
  4338.      </pre>
  4339.      </pre>
  4340.      <p>will not match the string `<samp><span class="samp">foo</span></samp>' because when the macro is expanded
  4341. the rule is equivalent to `<samp><span class="samp">foo[A-Z][A-Z0-9]*?</span></samp>'  and the precedence
  4342. is such that the `<samp><span class="samp">?</span></samp>' is associated with `<samp><span class="samp">[A-Z0-9]*</span></samp>'.  With
  4343. <code>flex</code>, the rule will be expanded to `<samp><span class="samp">foo([A-Z][A-Z0-9]*)?</span></samp>'
  4344. and so the string `<samp><span class="samp">foo</span></samp>' will match.
  4345.  
  4346.      <li>Note that if the definition begins with `<samp><span class="samp">^</span></samp>' or ends with `<samp><span class="samp">$</span></samp>'
  4347. then it is <em>not</em> expanded with parentheses, to allow these
  4348. operators to appear in definitions without losing their special
  4349. meanings.  But the `<samp><span class="samp"><s></span></samp>', `<samp><span class="samp">/</span></samp>', and <code><<EOF>></code> operators
  4350. cannot be used in a <code>flex</code> definition.
  4351.  
  4352.      <li>Using `<samp><span class="samp">-l</span></samp>' results in the <code>lex</code> behavior of no parentheses
  4353. around the definition.
  4354.  
  4355.      <li>The POSIX specification is that the definition be enclosed in parentheses.
  4356.  
  4357.      <li>Some implementations of <code>lex</code> allow a rule's action to begin on a
  4358. separate line, if the rule's pattern has trailing whitespace:
  4359.  
  4360.      <p><a name="index-patterns-and-actions-on-different-lines-452"></a>
  4361.      <pre class="example">     <pre class="verbatim">          
  4362.               %%
  4363.               foo|bar<space here>
  4364.                 { foobar_action();}
  4365.      </pre>
  4366.      </pre>
  4367.      <p><code>flex</code> does not support this feature.
  4368.  
  4369.      <li>The <code>lex</code> <code>%r</code> (generate a Ratfor scanner) option is not
  4370. supported.  It is not part of the POSIX specification.
  4371.  
  4372.      <li>After a call to <code>unput()</code>, <em>yytext</em> is undefined until the
  4373. next token is matched, unless the scanner was built using <code>%array</code>. 
  4374. This is not the case with <code>lex</code> or the POSIX specification.  The
  4375. `<samp><span class="samp">-l</span></samp>' option does away with this incompatibility.
  4376.  
  4377.      <li>The precedence of the `<samp><span class="samp">{,}</span></samp>' (numeric range) operator is
  4378. different.  The AT&T and POSIX specifications of <code>lex</code>
  4379. interpret `<samp><span class="samp">abc{1,3}</span></samp>' as match one, two,
  4380. or three occurrences of `<samp><span class="samp">abc</span></samp>'”, whereas <code>flex</code> interprets it
  4381. as “match `<samp><span class="samp">ab</span></samp>' followed by one, two, or three occurrences of
  4382. `<samp><span class="samp">c</span></samp>'”.  The `<samp><span class="samp">-l</span></samp>' and `<samp><span class="samp">--posix</span></samp>' options do away with this
  4383. incompatibility.
  4384.  
  4385.      <li>The precedence of the `<samp><span class="samp">^</span></samp>' operator is different.  <code>lex</code>
  4386. interprets `<samp><span class="samp">^foo|bar</span></samp>' as “match either 'foo' at the beginning of a
  4387. line, or 'bar' anywhere”, whereas <code>flex</code> interprets it as “match
  4388. either `<samp><span class="samp">foo</span></samp>' or `<samp><span class="samp">bar</span></samp>' if they come at the beginning of a
  4389. line”.  The latter is in agreement with the POSIX specification.
  4390.  
  4391.      <li>The special table-size declarations such as <code>%a</code> supported by
  4392. <code>lex</code> are not required by <code>flex</code> scanners..  <code>flex</code>
  4393. ignores them. 
  4394. <li>The name <code>FLEX_SCANNER</code> is <code>#define</code>'d so scanners may be
  4395. written for use with either <code>flex</code> or <code>lex</code>.  Scanners also
  4396. include <code>YY_FLEX_MAJOR_VERSION</code>,  <code>YY_FLEX_MINOR_VERSION</code>
  4397. and <code>YY_FLEX_SUBMINOR_VERSION</code>
  4398. indicating which version of <code>flex</code> generated the scanner. For
  4399. example, for the 2.5.22 release, these defines would be 2,  5 and 22
  4400. respectively. If the version of <code>flex</code> being used is a beta
  4401. version, then the symbol <code>FLEX_BETA</code> is defined. 
  4402. </ul>
  4403.  
  4404.    <p><a name="index-POSIX-comp_003bcompliance-453"></a><a name="index-non_002dPOSIX-features-of-flex-454"></a>The following <code>flex</code> features are not included in <code>lex</code> or the
  4405. POSIX specification:
  4406.  
  4407.      <ul>
  4408. <li>C++ scanners
  4409. <li>%option
  4410. <li>start condition scopes
  4411. <li>start condition stacks
  4412. <li>interactive/non-interactive scanners
  4413. <li>yy_scan_string() and friends
  4414. <li>yyterminate()
  4415. <li>yy_set_interactive()
  4416. <li>yy_set_bol()
  4417. <li>YY_AT_BOL()
  4418.    <<EOF>>
  4419. <li><*>
  4420. <li>YY_DECL
  4421. <li>YY_START
  4422. <li>YY_USER_ACTION
  4423. <li>YY_USER_INIT
  4424. <li>#line directives
  4425. <li>%{}'s around actions
  4426. <li>reentrant C API
  4427. <li>multiple actions on a line
  4428. <li>almost all of the <code>flex</code> command-line options
  4429. </ul>
  4430.  
  4431.    <p>The feature “multiple actions on a line”
  4432. refers to the fact that with <code>flex</code> you can put multiple actions on
  4433. the same line, separated with semi-colons, while with <code>lex</code>, the
  4434. following:
  4435.  
  4436. <pre class="example"><pre class="verbatim">     
  4437.          foo    handle_foo(); ++num_foos_seen;
  4438. </pre>
  4439. </pre>
  4440.    <p>is (rather surprisingly) truncated to
  4441.  
  4442. <pre class="example"><pre class="verbatim">     
  4443.          foo    handle_foo();
  4444. </pre>
  4445. </pre>
  4446.    <p><code>flex</code> does not truncate the action.  Actions that are not enclosed
  4447. in braces are simply terminated at the end of the line.
  4448.  
  4449. <div class="node">
  4450. <p><hr>
  4451. <a name="Memory-Management"></a>
  4452. Next: <a rel="next" accesskey="n" href="#Serialized-Tables">Serialized Tables</a>,
  4453. Previous: <a rel="previous" accesskey="p" href="#Lex-and-Posix">Lex and Posix</a>,
  4454. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  4455.  
  4456. </div>
  4457.  
  4458. <h2 class="chapter">21 Memory Management</h2>
  4459.  
  4460. <p><a name="index-memory-management-455"></a><a name="memory_002dmanagement"></a>
  4461. This chapter describes how flex handles dynamic memory, and how you can
  4462. override the default behavior.
  4463.  
  4464. <ul class="menu">
  4465. <li><a accesskey="1" href="#The-Default-Memory-Management">The Default Memory Management</a>
  4466. <li><a accesskey="2" href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a>
  4467. <li><a accesskey="3" href="#A-Note-About-yytext-And-Memory">A Note About yytext And Memory</a>
  4468. </ul>
  4469.  
  4470. <div class="node">
  4471. <p><hr>
  4472. <a name="The-Default-Memory-Management"></a>
  4473. Next: <a rel="next" accesskey="n" href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a>,
  4474. Previous: <a rel="previous" accesskey="p" href="#Memory-Management">Memory Management</a>,
  4475. Up: <a rel="up" accesskey="u" href="#Memory-Management">Memory Management</a>
  4476.  
  4477. </div>
  4478.  
  4479. <h3 class="section">21.1 The Default Memory Management</h3>
  4480.  
  4481. <p>Flex allocates dynamic memory during initialization, and once in a while from
  4482. within a call to yylex(). Initialization takes place during the first call to
  4483. yylex(). Thereafter, flex may reallocate more memory if it needs to enlarge a
  4484. buffer. As of version 2.5.9 Flex will clean up all memory when you call <code>yylex_destroy</code>
  4485. See <a href="#faq_002dmemory_002dleak">faq-memory-leak</a>.
  4486.  
  4487.    <p>Flex allocates dynamic memory for four purposes, listed below <a rel="footnote" href="#fn-2" name="fnd-2"><sup>2</sup></a>
  4488.  
  4489.      <dl>
  4490. <dt>16kB for the input buffer.<dd>Flex allocates memory for the character buffer used to perform pattern
  4491. matching.  Flex must read ahead from the input stream and store it in a large
  4492. character buffer.  This buffer is typically the largest chunk of dynamic memory
  4493. flex consumes. This buffer will grow if necessary, doubling the size each time. 
  4494. Flex frees this memory when you call yylex_destroy().  The default size of this
  4495. buffer (16384 bytes) is almost always too large.  The ideal size for this
  4496. buffer is the length of the longest token expected.  Flex will allocate a few
  4497. extra bytes for housekeeping.
  4498.  
  4499.      <br><dt>16kb for the REJECT state. This will only be allocated if you use REJECT.<dd>The size is the same as the input buffer, so if you override the size of the
  4500. input buffer, then you automatically override the size of this buffer as well.
  4501.  
  4502.      <br><dt>100 bytes for the start condition stack.<dd>Flex allocates memory for the start condition stack. This is the stack used
  4503. for pushing start states, i.e., with yy_push_state(). It will grow if
  4504. necessary.  Since the states are simply integers, this stack doesn't consume
  4505. much memory.  This stack is not present if <code>%option stack</code> is not
  4506. specified.  You will rarely need to tune this buffer. The ideal size for this
  4507. stack is the maximum depth expected.  The memory for this stack is
  4508. automatically destroyed when you call yylex_destroy(). See <a href="#option_002dstack">option-stack</a>.
  4509.  
  4510.      <br><dt>40 bytes for each YY_BUFFER_STATE.<dd>Flex allocates memory for each YY_BUFFER_STATE. The buffer state itself
  4511. is about 40 bytes, plus an additional large character buffer (described above.) 
  4512. The initial buffer state is created during initialization, and with each call
  4513. to yy_create_buffer(). You can't tune the size of this, but you can tune the
  4514. character buffer as described above. Any buffer state that you explicitly
  4515. create by calling yy_create_buffer() is <em>NOT</em> destroyed automatically. You
  4516. must call yy_delete_buffer() to free the memory. The exception to this rule is
  4517. that flex will delete the current buffer automatically when you call
  4518. yylex_destroy(). If you delete the current buffer, be sure to set it to NULL. 
  4519. That way, flex will not try to delete the buffer a second time (possibly
  4520. crashing your program!) At the time of this writing, flex does not provide a
  4521. growable stack for the buffer states.  You have to manage that yourself. 
  4522. See <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a>.
  4523.  
  4524.      <br><dt>84 bytes for the reentrant scanner guts<dd>Flex allocates about 84 bytes for the reentrant scanner structure when
  4525. you call yylex_init(). It is destroyed when the user calls yylex_destroy().
  4526.  
  4527.    </dl>
  4528.  
  4529. <div class="node">
  4530. <p><hr>
  4531. <a name="Overriding-The-Default-Memory-Management"></a>
  4532. Next: <a rel="next" accesskey="n" href="#A-Note-About-yytext-And-Memory">A Note About yytext And Memory</a>,
  4533. Previous: <a rel="previous" accesskey="p" href="#The-Default-Memory-Management">The Default Memory Management</a>,
  4534. Up: <a rel="up" accesskey="u" href="#Memory-Management">Memory Management</a>
  4535.  
  4536. </div>
  4537.  
  4538. <h3 class="section">21.2 Overriding The Default Memory Management</h3>
  4539.  
  4540. <p><a name="index-yyalloc_002c-overriding-456"></a><a name="index-yyrealloc_002c-overriding-457"></a><a name="index-yyfree_002c-overriding-458"></a>
  4541. Flex calls the functions <code>yyalloc</code>, <code>yyrealloc</code>, and <code>yyfree</code>
  4542. when it needs to allocate or free memory. By default, these functions are
  4543. wrappers around the standard C functions, <code>malloc</code>, <code>realloc</code>, and
  4544. <code>free</code>, respectively. You can override the default implementations by telling
  4545. flex that you will provide your own implementations.
  4546.  
  4547.    <p>To override the default implementations, you must do two things:
  4548.  
  4549.      <ol type=1 start=1>
  4550.  
  4551.      <li>Suppress the default implementations by specifying one or more of the
  4552. following options:
  4553.  
  4554.           
  4555. <a name="index-noyyalloc-459"></a>
  4556. <ul><li><code>%option noyyalloc</code>
  4557. <li><code>%option noyyrealloc</code>
  4558. <li><code>%option noyyfree</code>. 
  4559. </ul>
  4560.  
  4561.      <li>Provide your own implementation of the following functions: <a rel="footnote" href="#fn-3" name="fnd-3"><sup>3</sup></a>
  4562.  
  4563.      <pre class="example">     <pre class="verbatim">          
  4564.           // For a non-reentrant scanner
  4565.           void * yyalloc (size_t bytes);
  4566.           void * yyrealloc (void * ptr, size_t bytes);
  4567.           void   yyfree (void * ptr);
  4568.           
  4569.           // For a reentrant scanner
  4570.           void * yyalloc (size_t bytes, void * yyscanner);
  4571.           void * yyrealloc (void * ptr, size_t bytes, void * yyscanner);
  4572.           void   yyfree (void * ptr, void * yyscanner);
  4573.      </pre>
  4574.      </pre>
  4575.         </ol>
  4576.  
  4577.    <p>In the following example, we will override all three memory routines. We assume
  4578. that there is a custom allocator with garbage collection. In order to make this
  4579. example interesting, we will use a reentrant scanner, passing a pointer to the
  4580. custom allocator through <code>yyextra</code>.
  4581.  
  4582.    <p><a name="index-overriding-the-memory-routines-460"></a>
  4583. <pre class="example"><pre class="verbatim">     
  4584.      %{
  4585.      #include "some_allocator.h"
  4586.      %}
  4587.      
  4588.      /* Suppress the default implementations. */
  4589.      %option noyyalloc noyyrealloc noyyfree
  4590.      %option reentrant
  4591.      
  4592.      /* Initialize the allocator. */
  4593.      #define YY_EXTRA_TYPE  struct allocator*
  4594.      #define YY_USER_INIT  yyextra = allocator_create();
  4595.      
  4596.      %%
  4597.      .|\n   ;
  4598.      %%
  4599.      
  4600.      /* Provide our own implementations. */
  4601.      void * yyalloc (size_t bytes, void* yyscanner) {
  4602.          return allocator_alloc (yyextra, bytes);
  4603.      }
  4604.      
  4605.      void * yyrealloc (void * ptr, size_t bytes, void* yyscanner) {
  4606.          return allocator_realloc (yyextra, bytes);
  4607.      }
  4608.      
  4609.      void yyfree (void * ptr, void * yyscanner) {      
  4610.          /* Do nothing -- we leave it to the garbage collector. */
  4611.      }
  4612.      
  4613. </pre>
  4614. </pre>
  4615.    <div class="node">
  4616. <p><hr>
  4617. <a name="A-Note-About-yytext-And-Memory"></a>
  4618. Previous: <a rel="previous" accesskey="p" href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a>,
  4619. Up: <a rel="up" accesskey="u" href="#Memory-Management">Memory Management</a>
  4620.  
  4621. </div>
  4622.  
  4623. <h3 class="section">21.3 A Note About yytext And Memory</h3>
  4624.  
  4625. <p><a name="index-yytext_002c-memory-considerations-461"></a>
  4626. When flex finds a match, <code>yytext</code> points to the first character of the
  4627. match in the input buffer. The string itself is part of the input buffer, and
  4628. is <em>NOT</em> allocated separately. The value of yytext will be overwritten the next
  4629. time yylex() is called. In short, the value of yytext is only valid from within
  4630. the matched rule's action.
  4631.  
  4632.    <p>Often, you want the value of yytext to persist for later processing, i.e., by a
  4633. parser with non-zero lookahead. In order to preserve yytext, you will have to
  4634. copy it with strdup() or a similar function. But this introduces some headache
  4635. because your parser is now responsible for freeing the copy of yytext. If you
  4636. use a yacc or bison parser, (commonly used with flex), you will discover that
  4637. the error recovery mechanisms can cause memory to be leaked.
  4638.  
  4639.    <p>To prevent memory leaks from strdup'd yytext, you will have to track the memory
  4640. somehow. Our experience has shown that a garbage collection mechanism or a
  4641. pooled memory mechanism will save you a lot of grief when writing parsers.
  4642.  
  4643. <div class="node">
  4644. <p><hr>
  4645. <a name="Serialized-Tables"></a>
  4646. Next: <a rel="next" accesskey="n" href="#Diagnostics">Diagnostics</a>,
  4647. Previous: <a rel="previous" accesskey="p" href="#Memory-Management">Memory Management</a>,
  4648. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  4649.  
  4650. </div>
  4651.  
  4652. <h2 class="chapter">22 Serialized Tables</h2>
  4653.  
  4654. <p><a name="index-serialization-462"></a><a name="index-memory_002c-serialized-tables-463"></a>
  4655. <a name="serialization"></a>
  4656. A <code>flex</code> scanner has the ability to save the DFA tables to a file, and
  4657. load them at runtime when needed.  The motivation for this feature is to reduce
  4658. the runtime memory footprint.  Traditionally, these tables have been compiled into
  4659. the scanner as C arrays, and are sometimes quite large.  Since the tables are
  4660. compiled into the scanner, the memory used by the tables can never be freed. 
  4661. This is a waste of memory, especially if an application uses several scanners,
  4662. but none of them at the same time.
  4663.  
  4664.    <p>The serialization feature allows the tables to be loaded at runtime, before
  4665. scanning begins. The tables may be discarded when scanning is finished.
  4666.  
  4667. <ul class="menu">
  4668. <li><a accesskey="1" href="#Creating-Serialized-Tables">Creating Serialized Tables</a>
  4669. <li><a accesskey="2" href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a>
  4670. <li><a accesskey="3" href="#Tables-File-Format">Tables File Format</a>
  4671. </ul>
  4672.  
  4673. <div class="node">
  4674. <p><hr>
  4675. <a name="Creating-Serialized-Tables"></a>
  4676. Next: <a rel="next" accesskey="n" href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a>,
  4677. Previous: <a rel="previous" accesskey="p" href="#Serialized-Tables">Serialized Tables</a>,
  4678. Up: <a rel="up" accesskey="u" href="#Serialized-Tables">Serialized Tables</a>
  4679.  
  4680. </div>
  4681.  
  4682. <h3 class="section">22.1 Creating Serialized Tables</h3>
  4683.  
  4684. <p><a name="index-tables_002c-creating-serialized-464"></a><a name="index-serialization-of-tables-465"></a>
  4685. You may create a scanner with serialized tables by specifying:
  4686.  
  4687. <pre class="example"><pre class="verbatim">     
  4688.          %option tables-file=FILE
  4689.      or
  4690.          --tables-file=FILE
  4691. </pre>
  4692. </pre>
  4693.    <p>These options instruct flex to save the DFA tables to the file <var>FILE</var>. The tables
  4694. will <em>not</em> be embedded in the generated scanner. The scanner will not
  4695. function on its own. The scanner will be dependent upon the serialized tables. You must
  4696. load the tables from this file at runtime before you can scan anything.
  4697.  
  4698.    <p>If you do not specify a filename to <code>--tables-file</code>, the tables will be
  4699. saved to <samp><span class="file">lex.yy.tables</span></samp>, where `<samp><span class="samp">yy</span></samp>' is the appropriate prefix.
  4700.  
  4701.    <p>If your project uses several different scanners, you can concatenate the
  4702. serialized tables into one file, and flex will find the correct set of tables,
  4703. using the scanner prefix as part of the lookup key. An example follows:
  4704.  
  4705.    <p><a name="index-serialized-tables_002c-multiple-scanners-466"></a>
  4706. <pre class="example"><pre class="verbatim">     
  4707.      $ flex --tables-file --prefix=cpp cpp.l
  4708.      $ flex --tables-file --prefix=c   c.l
  4709.      $ cat lex.cpp.tables lex.c.tables  >  all.tables
  4710. </pre>
  4711. </pre>
  4712.    <p>The above example created two scanners, `<samp><span class="samp">cpp</span></samp>', and `<samp><span class="samp">c</span></samp>'. Since we did
  4713. not specify a filename, the tables were serialized to <samp><span class="file">lex.c.tables</span></samp> and
  4714. <samp><span class="file">lex.cpp.tables</span></samp>, respectively. Then, we concatenated the two files
  4715. together into <samp><span class="file">all.tables</span></samp>, which we will distribute with our project. At
  4716. runtime, we will open the file and tell flex to load the tables from it.  Flex
  4717. will find the correct tables automatically. (See next section).
  4718.  
  4719. <div class="node">
  4720. <p><hr>
  4721. <a name="Loading-and-Unloading-Serialized-Tables"></a>
  4722. Next: <a rel="next" accesskey="n" href="#Tables-File-Format">Tables File Format</a>,
  4723. Previous: <a rel="previous" accesskey="p" href="#Creating-Serialized-Tables">Creating Serialized Tables</a>,
  4724. Up: <a rel="up" accesskey="u" href="#Serialized-Tables">Serialized Tables</a>
  4725.  
  4726. </div>
  4727.  
  4728. <h3 class="section">22.2 Loading and Unloading Serialized Tables</h3>
  4729.  
  4730. <p><a name="index-tables_002c-loading-and-unloading-467"></a><a name="index-loading-tables-at-runtime-468"></a><a name="index-tables_002c-freeing-469"></a><a name="index-freeing-tables-470"></a><a name="index-memory_002c-serialized-tables-471"></a>
  4731. If you've built your scanner with <code>%option tables-file</code>, then you must
  4732. load the scanner tables at runtime. This can be accomplished with the following
  4733. function:
  4734.  
  4735. <div class="defun">
  4736. — Function: int <b>yytables_fload</b> (<var>FILE* fp </var>[<var>, yyscan_t scanner</var>])<var><a name="index-yytables_005ffload-472"></a></var><br>
  4737. <blockquote><p>Locates scanner tables in the stream pointed to by <var>fp</var> and loads them. 
  4738. Memory for the tables is allocated via <code>yyalloc</code>.  You must call this
  4739. function before the first call to <code>yylex</code>. The argument <var>scanner</var>
  4740. only appears in the reentrant scanner. 
  4741. This function returns `<samp><span class="samp">0</span></samp>' (zero) on success, or non-zero on error. 
  4742. </p></blockquote></div>
  4743.  
  4744.    <p>The loaded tables are <strong>not</strong> automatically destroyed (unloaded) when you
  4745. call <code>yylex_destroy</code>. The reason is that you may create several scanners
  4746. of the same type (in a reentrant scanner), each of which needs access to these
  4747. tables.  To avoid a nasty memory leak, you must call the following function:
  4748.  
  4749. <div class="defun">
  4750. — Function: int <b>yytables_destroy</b> ([<var>yyscan_t scanner</var>])<var><a name="index-yytables_005fdestroy-473"></a></var><br>
  4751. <blockquote><p>Unloads the scanner tables. The tables must be loaded again before you can scan
  4752. any more data.  The argument <var>scanner</var> only appears in the reentrant
  4753. scanner.  This function returns `<samp><span class="samp">0</span></samp>' (zero) on success, or non-zero on
  4754. error. 
  4755. </p></blockquote></div>
  4756.  
  4757.    <p><strong>The functions </strong><code>yytables_fload</code><strong> and </strong><code>yytables_destroy</code><strong> are not
  4758. thread-safe.</strong> You must ensure that these functions are called exactly once (for
  4759. each scanner type) in a threaded program, before any thread calls <code>yylex</code>. 
  4760. After the tables are loaded, they are never written to, and no thread
  4761. protection is required thereafter – until you destroy them.
  4762.  
  4763. <div class="node">
  4764. <p><hr>
  4765. <a name="Tables-File-Format"></a>
  4766. Previous: <a rel="previous" accesskey="p" href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a>,
  4767. Up: <a rel="up" accesskey="u" href="#Serialized-Tables">Serialized Tables</a>
  4768.  
  4769. </div>
  4770.  
  4771. <h3 class="section">22.3 Tables File Format</h3>
  4772.  
  4773. <p><a name="index-tables_002c-file-format-474"></a><a name="index-file-format_002c-serialized-tables-475"></a>
  4774. This section defines the file format of serialized <code>flex</code> tables.
  4775.  
  4776.    <p>The tables format allows for one or more sets of tables to be
  4777. specified, where each set corresponds to a given scanner. Scanners are
  4778. indexed by name, as described below. The file format is as follows:
  4779.  
  4780. <pre class="example"><pre class="verbatim">     
  4781.                       TABLE SET 1
  4782.                      +-------------------------------+
  4783.              Header  | uint32          th_magic;     |
  4784.                      | uint32          th_hsize;     |
  4785.                      | uint32          th_ssize;     |
  4786.                      | uint16          th_flags;     |
  4787.                      | char            th_version[]; |
  4788.                      | char            th_name[];    |
  4789.                      | uint8           th_pad64[];   |
  4790.                      +-------------------------------+
  4791.              Table 1 | uint16          td_id;        |
  4792.                      | uint16          td_flags;     |
  4793.                      | uint32          td_lolen;     |
  4794.                      | uint32          td_hilen;     |
  4795.                      | void            td_data[];    |
  4796.                      | uint8           td_pad64[];   |
  4797.                      +-------------------------------+
  4798.              Table 2 |                               |
  4799.                 .    .                               .
  4800.                 .    .                               .
  4801.                 .    .                               .
  4802.                 .    .                               .
  4803.              Table n |                               |
  4804.                      +-------------------------------+
  4805.                       TABLE SET 2
  4806.                            .
  4807.                            .
  4808.                            .
  4809.                       TABLE SET N
  4810. </pre>
  4811. </pre>
  4812.    <p>The above diagram shows that a complete set of tables consists of a header
  4813. followed by multiple individual tables. Furthermore, multiple complete sets may
  4814. be present in the same file, each set with its own header and tables. The sets
  4815. are contiguous in the file. The only way to know if another set follows is to
  4816. check the next four bytes for the magic number (or check for EOF). The header
  4817. and tables sections are padded to 64-bit boundaries. Below we describe each
  4818. field in detail. This format does not specify how the scanner will expand the
  4819. given data, i.e., data may be serialized as int8, but expanded to an int32
  4820. array at runtime. This is to reduce the size of the serialized data where
  4821. possible.  Remember, <em>all integer values are in network byte order</em>.
  4822.  
  4823. <p class="noindent">Fields of a table header:
  4824.  
  4825.      <dl>
  4826. <dt><code>th_magic</code><dd>Magic number, always 0xF13C57B1.
  4827.  
  4828.      <br><dt><code>th_hsize</code><dd>Size of this entire header, in bytes, including all fields plus any padding.
  4829.  
  4830.      <br><dt><code>th_ssize</code><dd>Size of this entire set, in bytes, including the header, all tables, plus
  4831. any padding.
  4832.  
  4833.      <br><dt><code>th_flags</code><dd>Bit flags for this table set. Currently unused.
  4834.  
  4835.      <br><dt><code>th_version[]</code><dd>Flex version in NULL-termninated string format. e.g., `<samp><span class="samp">2.5.13a</span></samp>'. This is
  4836. the version of flex that was used to create the serialized tables.
  4837.  
  4838.      <br><dt><code>th_name[]</code><dd>Contains the name of this table set. The default is `<samp><span class="samp">yytables</span></samp>',
  4839. and is prefixed accordingly, e.g., `<samp><span class="samp">footables</span></samp>'. Must be NULL-terminated.
  4840.  
  4841.      <br><dt><code>th_pad64[]</code><dd>Zero or more NULL bytes, padding the entire header to the next 64-bit boundary
  4842. as calculated from the beginning of the header. 
  4843. </dl>
  4844.  
  4845. <p class="noindent">Fields of a table:
  4846.  
  4847.      <dl>
  4848. <dt><code>td_id</code><dd>Specifies the table identifier. Possible values are:
  4849.           <dl>
  4850. <dt><code>YYTD_ID_ACCEPT (0x01)</code><dd><code>yy_accept</code>
  4851. <br><dt><code>YYTD_ID_BASE   (0x02)</code><dd><code>yy_base</code>
  4852. <br><dt><code>YYTD_ID_CHK    (0x03)</code><dd><code>yy_chk</code>
  4853. <br><dt><code>YYTD_ID_DEF    (0x04)</code><dd><code>yy_def</code>
  4854. <br><dt><code>YYTD_ID_EC     (0x05)</code><dd><code>yy_ec </code>
  4855. <br><dt><code>YYTD_ID_META   (0x06)</code><dd><code>yy_meta</code>
  4856. <br><dt><code>YYTD_ID_NUL_TRANS (0x07)</code><dd><code>yy_NUL_trans</code>
  4857. <br><dt><code>YYTD_ID_NXT (0x08)</code><dd><code>yy_nxt</code>. This array may be two dimensional. See the <code>td_hilen</code>
  4858. field below. 
  4859. <br><dt><code>YYTD_ID_RULE_CAN_MATCH_EOL (0x09)</code><dd><code>yy_rule_can_match_eol</code>
  4860. <br><dt><code>YYTD_ID_START_STATE_LIST (0x0A)</code><dd><code>yy_start_state_list</code>. This array is handled specially because it is an
  4861. array of pointers to structs. See the <code>td_flags</code> field below. 
  4862. <br><dt><code>YYTD_ID_TRANSITION (0x0B)</code><dd><code>yy_transition</code>. This array is handled specially because it is an array of
  4863. structs. See the <code>td_lolen</code> field below. 
  4864. <br><dt><code>YYTD_ID_ACCLIST (0x0C)</code><dd><code>yy_acclist</code>
  4865. </dl>
  4866.  
  4867.      <br><dt><code>td_flags</code><dd>Bit flags describing how to interpret the data in <code>td_data</code>. 
  4868. The data arrays are one-dimensional by default, but may be
  4869. two dimensional as specified in the <code>td_hilen</code> field.
  4870.  
  4871.           <dl>
  4872. <dt><code>YYTD_DATA8 (0x01)</code><dd>The data is serialized as an array of type int8. 
  4873. <br><dt><code>YYTD_DATA16 (0x02)</code><dd>The data is serialized as an array of type int16. 
  4874. <br><dt><code>YYTD_DATA32 (0x04)</code><dd>The data is serialized as an array of type int32. 
  4875. <br><dt><code>YYTD_PTRANS (0x08)</code><dd>The data is a list of indexes of entries in the expanded <code>yy_transition</code>
  4876. array.  Each index should be expanded to a pointer to the corresponding entry
  4877. in the <code>yy_transition</code> array. We count on the fact that the
  4878. <code>yy_transition</code> array has already been seen. 
  4879. <br><dt><code>YYTD_STRUCT (0x10)</code><dd>The data is a list of yy_trans_info structs, each of which consists of
  4880. two integers. There is no padding between struct elements or between structs. 
  4881. The type of each member is determined by the <code>YYTD_DATA*</code> bits. 
  4882. </dl>
  4883.  
  4884.      <br><dt><code>td_lolen</code><dd>Specifies the number of elements in the lowest dimension array. If this is
  4885. a one-dimensional array, then it is simply the number of elements in this array. 
  4886. The element size is determined by the <code>td_flags</code> field.
  4887.  
  4888.      <br><dt><code>td_hilen</code><dd>If <code>td_hilen</code> is non-zero, then the data is a two-dimensional array. 
  4889. Otherwise, the data is a one-dimensional array. <code>td_hilen</code> contains the
  4890. number of elements in the higher dimensional array, and <code>td_lolen</code> contains
  4891. the number of elements in the lowest dimension.
  4892.  
  4893.      <p>Conceptually, <code>td_data</code> is either <code>sometype td_data[td_lolen]</code>, or
  4894. <code>sometype td_data[td_hilen][td_lolen]</code>, where <code>sometype</code> is specified
  4895. by the <code>td_flags</code> field.  It is possible for both <code>td_lolen</code> and
  4896. <code>td_hilen</code> to be zero, in which case <code>td_data</code> is a zero length
  4897. array, and no data is loaded, i.e., this table is simply skipped. Flex does not
  4898. currently generate tables of zero length.
  4899.  
  4900.      <br><dt><code>td_data[]</code><dd>The table data. This array may be a one- or two-dimensional array, of type
  4901. <code>int8</code>, <code>int16</code>, <code>int32</code>, <code>struct yy_trans_info</code>, or
  4902. <code>struct yy_trans_info*</code>,  depending upon the values in the
  4903. <code>td_flags</code>, <code>td_lolen</code>, and <code>td_hilen</code> fields.
  4904.  
  4905.      <br><dt><code>td_pad64[]</code><dd>Zero or more NULL bytes, padding the entire table to the next 64-bit boundary as
  4906. calculated from the beginning of this table. 
  4907. </dl>
  4908.  
  4909. <div class="node">
  4910. <p><hr>
  4911. <a name="Diagnostics"></a>
  4912. Next: <a rel="next" accesskey="n" href="#Limitations">Limitations</a>,
  4913. Previous: <a rel="previous" accesskey="p" href="#Serialized-Tables">Serialized Tables</a>,
  4914. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  4915.  
  4916. </div>
  4917.  
  4918. <h2 class="chapter">23 Diagnostics</h2>
  4919.  
  4920. <p><a name="index-error-reporting_002c-diagnostic-messages-476"></a><a name="index-warnings_002c-diagnostic-messages-477"></a>
  4921. The following is a list of <code>flex</code> diagnostic messages:
  4922.  
  4923.      <ul>
  4924. <li>`<samp><span class="samp">warning, rule cannot be matched</span></samp>' indicates that the given rule
  4925. cannot be matched because it follows other rules that will always match
  4926. the same text as it.  For example, in the following `<samp><span class="samp">foo</span></samp>' cannot be
  4927. matched because it comes after an identifier “catch-all” rule:
  4928.  
  4929.      <p><a name="index-warning_002c-rule-cannot-be-matched-478"></a>
  4930.      <pre class="example">     <pre class="verbatim">          
  4931.               [a-z]+    got_identifier();
  4932.               foo       got_foo();
  4933.      </pre>
  4934.      </pre>
  4935.      <p>Using <code>REJECT</code> in a scanner suppresses this warning.
  4936.  
  4937.      <li>`<samp><span class="samp">warning, -s option given but default rule can be matched</span></samp>' means
  4938. that it is possible (perhaps only in a particular start condition) that
  4939. the default rule (match any single character) is the only one that will
  4940. match a particular input.  Since `<samp><span class="samp">-s</span></samp>' was given, presumably this is
  4941. not intended.
  4942.  
  4943.      <li><code>reject_used_but_not_detected undefined</code> or
  4944. <code>yymore_used_but_not_detected undefined</code>. These errors can occur
  4945. at compile time.  They indicate that the scanner uses <code>REJECT</code> or
  4946. <code>yymore()</code> but that <code>flex</code> failed to notice the fact, meaning
  4947. that <code>flex</code> scanned the first two sections looking for occurrences
  4948. of these actions and failed to find any, but somehow you snuck some in
  4949. (via a #include file, for example).  Use <code>%option reject</code> or
  4950. <code>%option yymore</code> to indicate to <code>flex</code> that you really do use
  4951. these features.
  4952.  
  4953.      <li>`<samp><span class="samp">flex scanner jammed</span></samp>'. a scanner compiled with
  4954. `<samp><span class="samp">-s</span></samp>' has encountered an input string which wasn't matched by any of
  4955. its rules.  This error can also occur due to internal problems.
  4956.  
  4957.      <li>`<samp><span class="samp">token too large, exceeds YYLMAX</span></samp>'. your scanner uses <code>%array</code>
  4958. and one of its rules matched a string longer than the <code>YYLMAX</code>
  4959. constant (8K bytes by default).  You can increase the value by
  4960. #define'ing <code>YYLMAX</code> in the definitions section of your <code>flex</code>
  4961. input.
  4962.  
  4963.      <li>`<samp><span class="samp">scanner requires -8 flag to use the character 'x'</span></samp>'. Your scanner
  4964. specification includes recognizing the 8-bit character `<samp><span class="samp">'x'</span></samp>' and
  4965. you did not specify the -8 flag, and your scanner defaulted to 7-bit
  4966. because you used the `<samp><span class="samp">-Cf</span></samp>' or `<samp><span class="samp">-CF</span></samp>' table compression options. 
  4967. See the discussion of the `<samp><span class="samp">-7</span></samp>' flag, <a href="#Scanner-Options">Scanner Options</a>, for
  4968. details.
  4969.  
  4970.      <li>`<samp><span class="samp">flex scanner push-back overflow</span></samp>'. you used <code>unput()</code> to push
  4971. back so much text that the scanner's buffer could not hold both the
  4972. pushed-back text and the current token in <code>yytext</code>.  Ideally the
  4973. scanner should dynamically resize the buffer in this case, but at
  4974. present it does not.
  4975.  
  4976.      <li>`<samp><span class="samp">input buffer overflow, can't enlarge buffer because scanner uses
  4977. REJECT</span></samp>'.  the scanner was working on matching an extremely large token
  4978. and needed to expand the input buffer.  This doesn't work with scanners
  4979. that use <code>REJECT</code>.
  4980.  
  4981.      <li>`<samp><span class="samp">fatal flex scanner internal error--end of buffer missed</span></samp>'. This can
  4982. occur in a scanner which is reentered after a long-jump has jumped out
  4983. (or over) the scanner's activation frame.  Before reentering the
  4984. scanner, use:
  4985.      <pre class="example">     <pre class="verbatim">          
  4986.               yyrestart( yyin );
  4987.      </pre>
  4988.      </pre>
  4989.      <p>or, as noted above, switch to using the C++ scanner class.
  4990.  
  4991.      <li>`<samp><span class="samp">too many start conditions in <> construct!</span></samp>'  you listed more start
  4992. conditions in a <> construct than exist (so you must have listed at
  4993. least one of them twice). 
  4994. </ul>
  4995.  
  4996. <div class="node">
  4997. <p><hr>
  4998. <a name="Limitations"></a>
  4999. Next: <a rel="next" accesskey="n" href="#Bibliography">Bibliography</a>,
  5000. Previous: <a rel="previous" accesskey="p" href="#Diagnostics">Diagnostics</a>,
  5001. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  5002.  
  5003. </div>
  5004.  
  5005. <h2 class="chapter">24 Limitations</h2>
  5006.  
  5007. <p><a name="index-limitations-of-flex-479"></a>
  5008. Some trailing context patterns cannot be properly matched and generate
  5009. warning messages (`<samp><span class="samp">dangerous trailing context</span></samp>').  These are
  5010. patterns where the ending of the first part of the rule matches the
  5011. beginning of the second part, such as `<samp><span class="samp">zx*/xy*</span></samp>', where the 'x*'
  5012. matches the 'x' at the beginning of the trailing context.  (Note that
  5013. the POSIX draft states that the text matched by such patterns is
  5014. undefined.)  For some trailing context rules, parts which are actually
  5015. fixed-length are not recognized as such, leading to the abovementioned
  5016. performance loss.  In particular, parts using `<samp><span class="samp">|</span></samp>' or `<samp><span class="samp">{n}</span></samp>'
  5017. (such as `<samp><span class="samp">foo{3}</span></samp>') are always considered variable-length. 
  5018. Combining trailing context with the special `<samp><span class="samp">|</span></samp>' action can result
  5019. in <em>fixed</em> trailing context being turned into the more expensive
  5020. <em>variable</em> trailing context.  For example, in the following:
  5021.  
  5022.    <p><a name="index-warning_002c-dangerous-trailing-context-480"></a>
  5023. <pre class="example"><pre class="verbatim">     
  5024.          %%
  5025.          abc      |
  5026.          xyz/def
  5027. </pre>
  5028. </pre>
  5029.    <p>Use of <code>unput()</code> invalidates yytext and yyleng, unless the
  5030. <code>%array</code> directive or the `<samp><span class="samp">-l</span></samp>' option has been used. 
  5031. Pattern-matching of <code>NUL</code>s is substantially slower than matching
  5032. other characters.  Dynamic resizing of the input buffer is slow, as it
  5033. entails rescanning all the text matched so far by the current (generally
  5034. huge) token.  Due to both buffering of input and read-ahead, you cannot
  5035. intermix calls to <samp><span class="file"><stdio.h></span></samp> routines, such as, <b>getchar()</b>,
  5036. with <code>flex</code> rules and expect it to work.  Call <code>input()</code>
  5037. instead.  The total table entries listed by the `<samp><span class="samp">-v</span></samp>' flag excludes
  5038. the number of table entries needed to determine what rule has been
  5039. matched.  The number of entries is equal to the number of DFA states if
  5040. the scanner does not use <code>REJECT</code>, and somewhat greater than the
  5041. number of states if it does.  <code>REJECT</code> cannot be used with the
  5042. `<samp><span class="samp">-f</span></samp>' or `<samp><span class="samp">-F</span></samp>' options.
  5043.  
  5044.    <p>The <code>flex</code> internal algorithms need documentation.
  5045.  
  5046. <div class="node">
  5047. <p><hr>
  5048. <a name="Bibliography"></a>
  5049. Next: <a rel="next" accesskey="n" href="#FAQ">FAQ</a>,
  5050. Previous: <a rel="previous" accesskey="p" href="#Limitations">Limitations</a>,
  5051. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  5052.  
  5053. </div>
  5054.  
  5055. <h2 class="chapter">25 Additional Reading</h2>
  5056.  
  5057. <p>You may wish to read more about the following programs:
  5058.      <ul>
  5059. <li>lex
  5060. <li>yacc
  5061. <li>sed
  5062. <li>awk
  5063. </ul>
  5064.  
  5065.    <p>The following books may contain material of interest:
  5066.  
  5067.    <p>John Levine, Tony Mason, and Doug Brown,
  5068. <em>Lex & Yacc</em>,
  5069. O'Reilly and Associates.  Be sure to get the 2nd edition.
  5070.  
  5071.    <p>M. E. Lesk and E. Schmidt,
  5072. <em>LEX – Lexical Analyzer Generator</em>
  5073.  
  5074.    <p>Alfred Aho, Ravi Sethi and Jeffrey Ullman, <em>Compilers: Principles,
  5075. Techniques and Tools</em>, Addison-Wesley (1986).  Describes the
  5076. pattern-matching techniques used by <code>flex</code> (deterministic finite
  5077. automata).
  5078.  
  5079. <div class="node">
  5080. <p><hr>
  5081. <a name="FAQ"></a>
  5082. Next: <a rel="next" accesskey="n" href="#Appendices">Appendices</a>,
  5083. Previous: <a rel="previous" accesskey="p" href="#Bibliography">Bibliography</a>,
  5084. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  5085.  
  5086. </div>
  5087.  
  5088. <h2 class="unnumbered">FAQ</h2>
  5089.  
  5090. <p>From time to time, the <code>flex</code> maintainer receives certain
  5091. questions. Rather than repeat answers to well-understood problems, we
  5092. publish them here.
  5093.  
  5094. <ul class="menu">
  5095. <li><a accesskey="1" href="#When-was-flex-born_003f">When was flex born?</a>
  5096. <li><a accesskey="2" href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>
  5097. <li><a accesskey="3" href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>
  5098. <li><a accesskey="4" href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>
  5099. <li><a accesskey="5" href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>
  5100. <li><a accesskey="6" href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>
  5101. <li><a accesskey="7" href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>
  5102. <li><a accesskey="8" href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>
  5103. <li><a accesskey="9" href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>
  5104. <li><a href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>
  5105. <li><a href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>
  5106. <li><a href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why cant I use fast or full tables with interactive mode?</a>
  5107. <li><a href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>
  5108. <li><a href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar cant I just parse it with flex?</a>
  5109. <li><a href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesnt yyrestart() set the start state back to INITIAL?</a>
  5110. <li><a href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>
  5111. <li><a href="#The-period-isnt-working-the-way-I-expected_002e">The period isnt working the way I expected.</a>
  5112. <li><a href="#Can-I-get-the-flex-manual-in-another-format_003f">Can I get the flex manual in another format?</a>
  5113. <li><a href="#Does-there-exist-a-_0022faster_0022-NDFA_002d_003eDFA-algorithm_003f">Does there exist a "faster" NDFA->DFA algorithm?</a>
  5114. <li><a href="#How-does-flex-compile-the-DFA-so-quickly_003f">How does flex compile the DFA so quickly?</a>
  5115. <li><a href="#How-can-I-use-more-than-8192-rules_003f">How can I use more than 8192 rules?</a>
  5116. <li><a href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>
  5117. <li><a href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>
  5118. <li><a href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>
  5119. <li><a href="#Where-else-can-I-find-help_003f">Where else can I find help?</a>
  5120. <li><a href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>
  5121. <li><a href="#I-get-an-error-about-undefined-yywrap_0028_0029_002e">I get an error about undefined yywrap().</a>
  5122. <li><a href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>
  5123. <li><a href="#How-can-I-expand-macros-in-the-input_003f">How can I expand macros in the input?</a>
  5124. <li><a href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>
  5125. <li><a href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>
  5126. <li><a href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>
  5127. <li><a href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>
  5128. <li><a href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>
  5129. <li><a href="#Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f">Why doesnt flex have non-greedy operators like perl does?</a>
  5130. <li><a href="#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e">Memory leak - 16386 bytes allocated by malloc.</a>
  5131. <li><a href="#How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f">How do I track the byte offset for lseek()?</a>
  5132. <li><a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>
  5133. <li><a href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>
  5134. <li><a href="#deleteme00">deleteme00</a>
  5135. <li><a href="#Are-certain-equivalent-patterns-faster-than-others_003f">Are certain equivalent patterns faster than others?</a>
  5136. <li><a href="#Is-backing-up-a-big-deal_003f">Is backing up a big deal?</a>
  5137. <li><a href="#Can-I-fake-multi_002dbyte-character-support_003f">Can I fake multi-byte character support?</a>
  5138. <li><a href="#deleteme01">deleteme01</a>
  5139. <li><a href="#Can-you-discuss-some-flex-internals_003f">Can you discuss some flex internals?</a>
  5140. <li><a href="#unput_0028_0029-messes-up-yy_005fat_005fbol">unput() messes up yy_at_bol</a>
  5141. <li><a href="#The-_007c-operator-is-not-doing-what-I-want">The | operator is not doing what I want</a>
  5142. <li><a href="#Why-can_0027t-flex-understand-this-variable-trailing-context-pattern_003f">Why can't flex understand this variable trailing context pattern?</a>
  5143. <li><a href="#The-_005e-operator-isn_0027t-working">The ^ operator isn't working</a>
  5144. <li><a href="#Trailing-context-is-getting-confused-with-trailing-optional-patterns">Trailing context is getting confused with trailing optional patterns</a>
  5145. <li><a href="#Is-flex-GNU-or-not_003f">Is flex GNU or not?</a>
  5146. <li><a href="#ERASEME53">ERASEME53</a>
  5147. <li><a href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>
  5148. <li><a href="#ERASEME55">ERASEME55</a>
  5149. <li><a href="#ERASEME56">ERASEME56</a>
  5150. <li><a href="#ERASEME57">ERASEME57</a>
  5151. <li><a href="#Is-there-a-repository-for-flex-scanners_003f">Is there a repository for flex scanners?</a>
  5152. <li><a href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>
  5153. <li><a href="#Where-can-I-find-grammars-for-lex-and-yacc_003f">Where can I find grammars for lex and yacc?</a>
  5154. <li><a href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>
  5155. <li><a href="#unnamed_002dfaq_002d62">unnamed-faq-62</a>
  5156. <li><a href="#unnamed_002dfaq_002d63">unnamed-faq-63</a>
  5157. <li><a href="#unnamed_002dfaq_002d64">unnamed-faq-64</a>
  5158. <li><a href="#unnamed_002dfaq_002d65">unnamed-faq-65</a>
  5159. <li><a href="#unnamed_002dfaq_002d66">unnamed-faq-66</a>
  5160. <li><a href="#unnamed_002dfaq_002d67">unnamed-faq-67</a>
  5161. <li><a href="#unnamed_002dfaq_002d68">unnamed-faq-68</a>
  5162. <li><a href="#unnamed_002dfaq_002d69">unnamed-faq-69</a>
  5163. <li><a href="#unnamed_002dfaq_002d70">unnamed-faq-70</a>
  5164. <li><a href="#unnamed_002dfaq_002d71">unnamed-faq-71</a>
  5165. <li><a href="#unnamed_002dfaq_002d72">unnamed-faq-72</a>
  5166. <li><a href="#unnamed_002dfaq_002d73">unnamed-faq-73</a>
  5167. <li><a href="#unnamed_002dfaq_002d74">unnamed-faq-74</a>
  5168. <li><a href="#unnamed_002dfaq_002d75">unnamed-faq-75</a>
  5169. <li><a href="#unnamed_002dfaq_002d76">unnamed-faq-76</a>
  5170. <li><a href="#unnamed_002dfaq_002d77">unnamed-faq-77</a>
  5171. <li><a href="#unnamed_002dfaq_002d78">unnamed-faq-78</a>
  5172. <li><a href="#unnamed_002dfaq_002d79">unnamed-faq-79</a>
  5173. <li><a href="#unnamed_002dfaq_002d80">unnamed-faq-80</a>
  5174. <li><a href="#unnamed_002dfaq_002d81">unnamed-faq-81</a>
  5175. <li><a href="#unnamed_002dfaq_002d82">unnamed-faq-82</a>
  5176. <li><a href="#unnamed_002dfaq_002d83">unnamed-faq-83</a>
  5177. <li><a href="#unnamed_002dfaq_002d84">unnamed-faq-84</a>
  5178. <li><a href="#unnamed_002dfaq_002d85">unnamed-faq-85</a>
  5179. <li><a href="#unnamed_002dfaq_002d86">unnamed-faq-86</a>
  5180. <li><a href="#unnamed_002dfaq_002d87">unnamed-faq-87</a>
  5181. <li><a href="#unnamed_002dfaq_002d88">unnamed-faq-88</a>
  5182. <li><a href="#unnamed_002dfaq_002d90">unnamed-faq-90</a>
  5183. <li><a href="#unnamed_002dfaq_002d91">unnamed-faq-91</a>
  5184. <li><a href="#unnamed_002dfaq_002d92">unnamed-faq-92</a>
  5185. <li><a href="#unnamed_002dfaq_002d93">unnamed-faq-93</a>
  5186. <li><a href="#unnamed_002dfaq_002d94">unnamed-faq-94</a>
  5187. <li><a href="#unnamed_002dfaq_002d95">unnamed-faq-95</a>
  5188. <li><a href="#unnamed_002dfaq_002d96">unnamed-faq-96</a>
  5189. <li><a href="#unnamed_002dfaq_002d97">unnamed-faq-97</a>
  5190. <li><a href="#unnamed_002dfaq_002d98">unnamed-faq-98</a>
  5191. <li><a href="#unnamed_002dfaq_002d99">unnamed-faq-99</a>
  5192. <li><a href="#unnamed_002dfaq_002d100">unnamed-faq-100</a>
  5193. <li><a href="#unnamed_002dfaq_002d101">unnamed-faq-101</a>
  5194. </ul>
  5195.  
  5196. <div class="node">
  5197. <p><hr>
  5198. <a name="When-was-flex-born%3f"></a>
  5199. <a name="When-was-flex-born_003f"></a>
  5200. Next: <a rel="next" accesskey="n" href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>,
  5201. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5202.  
  5203. </div>
  5204.  
  5205. <h3 class="unnumberedsec">When was flex born?</h3>
  5206.  
  5207. <p>Vern Paxson took over
  5208. the <cite>Software Tools</cite> lex project from Jef Poskanzer in 1982.  At that point it
  5209. was written in Ratfor.  Around 1987 or so, Paxson translated it into C, and
  5210. a legend was born :-).
  5211.  
  5212. <div class="node">
  5213. <p><hr>
  5214. <a name="How-do-I-expand-%5c-escape-sequences-in-C-style-quoted-strings%3f"></a>
  5215. <a name="How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f"></a>
  5216. Next: <a rel="next" accesskey="n" href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>,
  5217. Previous: <a rel="previous" accesskey="p" href="#When-was-flex-born_003f">When was flex born?</a>,
  5218. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5219.  
  5220. </div>
  5221.  
  5222. <h3 class="unnumberedsec">How do I expand \ escape sequences in C-style quoted strings?</h3>
  5223.  
  5224. <p>A key point when scanning quoted strings is that you cannot (easily) write
  5225. a single rule that will precisely match the string if you allow things
  5226. like embedded escape sequences and newlines.  If you try to match strings
  5227. with a single rule then you'll wind up having to rescan the string anyway
  5228. to find any escape sequences.
  5229.  
  5230.    <p>Instead you can use exclusive start conditions and a set of rules, one for
  5231. matching non-escaped text, one for matching a single escape, one for
  5232. matching an embedded newline, and one for recognizing the end of the
  5233. string.  Each of these rules is then faced with the question of where to
  5234. put its intermediary results.  The best solution is for the rules to
  5235. append their local value of <code>yytext</code> to the end of a “string literal”
  5236. buffer.  A rule like the escape-matcher will append to the buffer the
  5237. meaning of the escape sequence rather than the literal text in <code>yytext</code>. 
  5238. In this way, <code>yytext</code> does not need to be modified at all.
  5239.  
  5240. <div class="node">
  5241. <p><hr>
  5242. <a name="Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible%3f"></a>
  5243. <a name="Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f"></a>
  5244. Next: <a rel="next" accesskey="n" href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>,
  5245. Previous: <a rel="previous" accesskey="p" href="#How-do-I-expand-_005c-escape-sequences-in-C_002dstyle-quoted-strings_003f">How do I expand \ escape sequences in C-style quoted strings?</a>,
  5246. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5247.  
  5248. </div>
  5249.  
  5250. <h3 class="unnumberedsec">Why do flex scanners call fileno if it is not ANSI compatible?</h3>
  5251.  
  5252. <p>Flex scanners call <code>fileno()</code> in order to get the file descriptor
  5253. corresponding to <code>yyin</code>. The file descriptor may be passed to
  5254. <code>isatty()</code> or <code>read()</code>, depending upon which <code>%options</code> you specified. 
  5255. If your system does not have <code>fileno()</code> support, to get rid of the
  5256. <code>read()</code> call, do not specify <code>%option read</code>. To get rid of the <code>isatty()</code>
  5257. call, you must specify one of <code>%option always-interactive</code> or
  5258. <code>%option never-interactive</code>.
  5259.  
  5260. <div class="node">
  5261. <p><hr>
  5262. <a name="Does-flex-support-recursive-pattern-definitions%3f"></a>
  5263. <a name="Does-flex-support-recursive-pattern-definitions_003f"></a>
  5264. Next: <a rel="next" accesskey="n" href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>,
  5265. Previous: <a rel="previous" accesskey="p" href="#Why-do-flex-scanners-call-fileno-if-it-is-not-ANSI-compatible_003f">Why do flex scanners call fileno if it is not ANSI compatible?</a>,
  5266. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5267.  
  5268. </div>
  5269.  
  5270. <h3 class="unnumberedsec">Does flex support recursive pattern definitions?</h3>
  5271.  
  5272. <p>e.g.,
  5273.  
  5274. <pre class="example"><pre class="verbatim">     
  5275.      %%
  5276.      block   "{"({block}|{statement})*"}"
  5277. </pre>
  5278. </pre>
  5279.    <p>No. You cannot have recursive definitions.  The pattern-matching power of
  5280. regular expressions in general (and therefore flex scanners, too) is
  5281. limited.  In particular, regular expressions cannot “balance” parentheses
  5282. to an arbitrary degree.  For example, it's impossible to write a regular
  5283. expression that matches all strings containing the same number of '{'s
  5284. as '}'s.  For more powerful pattern matching, you need a parser, such
  5285. as <cite>GNU bison</cite>.
  5286.  
  5287. <div class="node">
  5288. <p><hr>
  5289. <a name="How-do-I-skip-huge-chunks-of-input-(tens-of-megabytes)-while-using-flex%3f"></a>
  5290. <a name="How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f"></a>
  5291. Next: <a rel="next" accesskey="n" href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>,
  5292. Previous: <a rel="previous" accesskey="p" href="#Does-flex-support-recursive-pattern-definitions_003f">Does flex support recursive pattern definitions?</a>,
  5293. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5294.  
  5295. </div>
  5296.  
  5297. <h3 class="unnumberedsec">How do I skip huge chunks of input (tens of megabytes) while using flex?</h3>
  5298.  
  5299. <p>Use <code>fseek()</code> (or <code>lseek()</code>) to position yyin, then call <code>yyrestart()</code>.
  5300.  
  5301. <div class="node">
  5302. <p><hr>
  5303. <a name="Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them."></a>
  5304. <a name="Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e"></a>
  5305. Next: <a rel="next" accesskey="n" href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>,
  5306. Previous: <a rel="previous" accesskey="p" href="#How-do-I-skip-huge-chunks-of-input-_0028tens-of-megabytes_0029-while-using-flex_003f">How do I skip huge chunks of input (tens of megabytes) while using flex?</a>,
  5307. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5308.  
  5309. </div>
  5310.  
  5311. <h3 class="unnumberedsec">Flex is not matching my patterns in the same order that I defined them.</h3>
  5312.  
  5313. <p><code>flex</code> picks the
  5314. rule that matches the most text (i.e., the longest possible input string). 
  5315. This is because <code>flex</code> uses an entirely different matching technique
  5316. (“deterministic finite automata”) that actually does all of the matching
  5317. simultaneously, in parallel.  (Seems impossible, but it's actually a fairly
  5318. simple technique once you understand the principles.)
  5319.  
  5320.    <p>A side-effect of this parallel matching is that when the input matches more
  5321. than one rule, <code>flex</code> scanners pick the rule that matched the <em>most</em> text. This
  5322. is explained further in the manual, in the section See <a href="#Matching">Matching</a>.
  5323.  
  5324.    <p>If you want <code>flex</code> to choose a shorter match, then you can work around this
  5325. behavior by expanding your short
  5326. rule to match more text, then put back the extra:
  5327.  
  5328. <pre class="example"><pre class="verbatim">     
  5329.      data_.*        yyless( 5 ); BEGIN BLOCKIDSTATE;
  5330. </pre>
  5331. </pre>
  5332.    <p>Another fix would be to make the second rule active only during the
  5333. <code><BLOCKIDSTATE></code> start condition, and make that start condition exclusive
  5334. by declaring it with <code>%x</code> instead of <code>%s</code>.
  5335.  
  5336.    <p>A final fix is to change the input language so that the ambiguity for
  5337. `<samp><span class="samp">data_</span></samp>' is removed, by adding characters to it that don't match the
  5338. identifier rule, or by removing characters (such as `<samp><span class="samp">_</span></samp>') from the
  5339. identifier rule so it no longer matches `<samp><span class="samp">data_</span></samp>'.  (Of course, you might
  5340. also not have the option of changing the input language.)
  5341.  
  5342. <div class="node">
  5343. <p><hr>
  5344. <a name="My-actions-are-executing-out-of-order-or-sometimes-not-at-all."></a>
  5345. <a name="My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e"></a>
  5346. Next: <a rel="next" accesskey="n" href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>,
  5347. Previous: <a rel="previous" accesskey="p" href="#Flex-is-not-matching-my-patterns-in-the-same-order-that-I-defined-them_002e">Flex is not matching my patterns in the same order that I defined them.</a>,
  5348. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5349.  
  5350. </div>
  5351.  
  5352. <h3 class="unnumberedsec">My actions are executing out of order or sometimes not at all.</h3>
  5353.  
  5354. <p>Most likely, you have (in error) placed the opening `<samp><span class="samp">{</span></samp>' of the action
  5355. block on a different line than the rule, e.g.,
  5356.  
  5357. <pre class="example"><pre class="verbatim">     
  5358.      ^(foo|bar)
  5359.      {  <<<--- WRONG!
  5360.      
  5361.      }
  5362. </pre>
  5363. </pre>
  5364.    <p><code>flex</code> requires that the opening `<samp><span class="samp">{</span></samp>' of an action associated with a rule
  5365. begin on the same line as does the rule.  You need instead to write your rules
  5366. as follows:
  5367.  
  5368. <pre class="example"><pre class="verbatim">     
  5369.      ^(foo|bar)   {  // CORRECT!
  5370.      
  5371.      }
  5372. </pre>
  5373. </pre>
  5374.    <div class="node">
  5375. <p><hr>
  5376. <a name="How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time%3f"></a>
  5377. <a name="How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f"></a>
  5378. Next: <a rel="next" accesskey="n" href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>,
  5379. Previous: <a rel="previous" accesskey="p" href="#My-actions-are-executing-out-of-order-or-sometimes-not-at-all_002e">My actions are executing out of order or sometimes not at all.</a>,
  5380. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5381.  
  5382. </div>
  5383.  
  5384. <h3 class="unnumberedsec">How can I have multiple input sources feed into the same scanner at the same time?</h3>
  5385.  
  5386. <p>If <small class="dots">...</small>
  5387.      <ul>
  5388. <li>your scanner is free of backtracking (verified using <code>flex</code>'s `<samp><span class="samp">-b</span></samp>' flag),
  5389. <li>AND you run your scanner interactively (`<samp><span class="samp">-I</span></samp>' option; default unless using special table
  5390. compression options),
  5391. <li>AND you feed it one character at a time by redefining <code>YY_INPUT</code> to do so,
  5392. </ul>
  5393.  
  5394.    <p>then every time it matches a token, it will have exhausted its input
  5395. buffer (because the scanner is free of backtracking).  This means you
  5396. can safely use <code>select()</code> at the point and only call <code>yylex()</code> for another
  5397. token if <code>select()</code> indicates there's data available.
  5398.  
  5399.    <p>That is, move the <code>select()</code> out from the input function to a point where
  5400. it determines whether <code>yylex()</code> gets called for the next token.
  5401.  
  5402.    <p>With this approach, you will still have problems if your input can arrive
  5403. piecemeal; <code>select()</code> could inform you that the beginning of a token is
  5404. available, you call <code>yylex()</code> to get it, but it winds up blocking waiting
  5405. for the later characters in the token.
  5406.  
  5407.    <p>Here's another way:  Move your input multiplexing inside of <code>YY_INPUT</code>.  That
  5408. is, whenever <code>YY_INPUT</code> is called, it <code>select()</code>'s to see where input is
  5409. available.  If input is available for the scanner, it reads and returns the
  5410. next byte.  If input is available from another source, it calls whatever
  5411. function is responsible for reading from that source.  (If no input is
  5412. available, it blocks until some input is available.)  I've used this technique in an
  5413. interpreter I wrote that both reads keyboard input using a <code>flex</code> scanner and
  5414. IPC traffic from sockets, and it works fine.
  5415.  
  5416. <div class="node">
  5417. <p><hr>
  5418. <a name="Can-I-build-nested-parsers-that-work-with-the-same-input-file%3f"></a>
  5419. <a name="Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f"></a>
  5420. Next: <a rel="next" accesskey="n" href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>,
  5421. Previous: <a rel="previous" accesskey="p" href="#How-can-I-have-multiple-input-sources-feed-into-the-same-scanner-at-the-same-time_003f">How can I have multiple input sources feed into the same scanner at the same time?</a>,
  5422. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5423.  
  5424. </div>
  5425.  
  5426. <h3 class="unnumberedsec">Can I build nested parsers that work with the same input file?</h3>
  5427.  
  5428. <p>This is not going to work without some additional effort.  The reason is
  5429. that <code>flex</code> block-buffers the input it reads from <code>yyin</code>.  This means that the
  5430. “outermost” <code>yylex()</code>, when called, will automatically slurp up the first 8K
  5431. of input available on yyin, and subsequent calls to other <code>yylex()</code>'s won't
  5432. see that input.  You might be tempted to work around this problem by
  5433. redefining <code>YY_INPUT</code> to only return a small amount of text, but it turns out
  5434. that that approach is quite difficult.  Instead, the best solution is to
  5435. combine all of your scanners into one large scanner, using a different
  5436. exclusive start condition for each.
  5437.  
  5438. <div class="node">
  5439. <p><hr>
  5440. <a name="How-can-I-match-text-only-at-the-end-of-a-file%3f"></a>
  5441. <a name="How-can-I-match-text-only-at-the-end-of-a-file_003f"></a>
  5442. Next: <a rel="next" accesskey="n" href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>,
  5443. Previous: <a rel="previous" accesskey="p" href="#Can-I-build-nested-parsers-that-work-with-the-same-input-file_003f">Can I build nested parsers that work with the same input file?</a>,
  5444. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5445.  
  5446. </div>
  5447.  
  5448. <h3 class="unnumberedsec">How can I match text only at the end of a file?</h3>
  5449.  
  5450. <p>There is no way to write a rule which is “match this text, but only if
  5451. it comes at the end of the file”.  You can fake it, though, if you happen
  5452. to have a character lying around that you don't allow in your input. 
  5453. Then you redefine <code>YY_INPUT</code> to call your own routine which, if it sees
  5454. an `<samp><span class="samp">EOF</span></samp>', returns the magic character first (and remembers to return a
  5455. real <code>EOF</code> next time it's called).  Then you could write:
  5456.  
  5457. <pre class="example"><pre class="verbatim">     
  5458.      <COMMENT>(.|\n)*{EOF_CHAR}    /* saw comment at EOF */
  5459. </pre>
  5460. </pre>
  5461.    <div class="node">
  5462. <p><hr>
  5463. <a name="How-can-I-make-REJECT-cascade-across-start-condition-boundaries%3f"></a>
  5464. <a name="How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f"></a>
  5465. Next: <a rel="next" accesskey="n" href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why cant I use fast or full tables with interactive mode?</a>,
  5466. Previous: <a rel="previous" accesskey="p" href="#How-can-I-match-text-only-at-the-end-of-a-file_003f">How can I match text only at the end of a file?</a>,
  5467. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5468.  
  5469. </div>
  5470.  
  5471. <h3 class="unnumberedsec">How can I make REJECT cascade across start condition boundaries?</h3>
  5472.  
  5473. <p>You can do this as follows.  Suppose you have a start condition `<samp><span class="samp">A</span></samp>', and
  5474. after exhausting all of the possible matches in `<samp><span class="samp"><A></span></samp>', you want to try
  5475. matches in `<samp><span class="samp"><INITIAL></span></samp>'.  Then you could use the following:
  5476.  
  5477. <pre class="example"><pre class="verbatim">     
  5478.      %x A
  5479.      %%
  5480.      <A>rule_that_is_long    ...; REJECT;
  5481.      <A>rule                 ...; REJECT; /* shorter rule */
  5482.      <A>etc.
  5483.      ...
  5484.      <A>.|\n  {
  5485.      /* Shortest and last rule in <A>, so
  5486.      * cascaded REJECT's will eventually
  5487.      * wind up matching this rule.  We want
  5488.      * to now switch to the initial state
  5489.      * and try matching from there instead.
  5490.      */
  5491.      yyless(0);    /* put back matched text */
  5492.      BEGIN(INITIAL);
  5493.      }
  5494. </pre>
  5495. </pre>
  5496.    <div class="node">
  5497. <p><hr>
  5498. <a name="Why-cant-I-use-fast-or-full-tables-with-interactive-mode%3f"></a>
  5499. <a name="Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f"></a>
  5500. Next: <a rel="next" accesskey="n" href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>,
  5501. Previous: <a rel="previous" accesskey="p" href="#How-can-I-make-REJECT-cascade-across-start-condition-boundaries_003f">How can I make REJECT cascade across start condition boundaries?</a>,
  5502. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5503.  
  5504. </div>
  5505.  
  5506. <h3 class="unnumberedsec">Why can't I use fast or full tables with interactive mode?</h3>
  5507.  
  5508. <p>One of the assumptions
  5509. flex makes is that interactive applications are inherently slow (they're
  5510. waiting on a human after all). 
  5511. It has to do with how the scanner detects that it must be finished scanning
  5512. a token.  For interactive scanners, after scanning each character the current
  5513. state is looked up in a table (essentially) to see whether there's a chance
  5514. of another input character possibly extending the length of the match.  If
  5515. not, the scanner halts.  For non-interactive scanners, the end-of-token test
  5516. is much simpler, basically a compare with 0, so no memory bus cycles.  Since
  5517. the test occurs in the innermost scanning loop, one would like to make it go
  5518. as fast as possible.
  5519.  
  5520.    <p>Still, it seems reasonable to allow the user to choose to trade off a bit
  5521. of performance in this area to gain the corresponding flexibility.  There
  5522. might be another reason, though, why fast scanners don't support the
  5523. interactive option.
  5524.  
  5525. <div class="node">
  5526. <p><hr>
  5527. <a name="How-much-faster-is--F-or--f-than--C%3f"></a>
  5528. <a name="How-much-faster-is-_002dF-or-_002df-than-_002dC_003f"></a>
  5529. Next: <a rel="next" accesskey="n" href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar cant I just parse it with flex?</a>,
  5530. Previous: <a rel="previous" accesskey="p" href="#Why-cant-I-use-fast-or-full-tables-with-interactive-mode_003f">Why cant I use fast or full tables with interactive mode?</a>,
  5531. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5532.  
  5533. </div>
  5534.  
  5535. <h3 class="unnumberedsec">How much faster is -F or -f than -C?</h3>
  5536.  
  5537. <p>Much faster (factor of 2-3).
  5538.  
  5539. <div class="node">
  5540. <p><hr>
  5541. <a name="If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex%3f"></a>
  5542. <a name="If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f"></a>
  5543. Next: <a rel="next" accesskey="n" href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesnt yyrestart() set the start state back to INITIAL?</a>,
  5544. Previous: <a rel="previous" accesskey="p" href="#How-much-faster-is-_002dF-or-_002df-than-_002dC_003f">How much faster is -F or -f than -C?</a>,
  5545. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5546.  
  5547. </div>
  5548.  
  5549. <h3 class="unnumberedsec">If I have a simple grammar can't I just parse it with flex?</h3>
  5550.  
  5551. <p>Is your grammar recursive? That's almost always a sign that you're
  5552. better off using a parser/scanner rather than just trying to use a scanner
  5553. alone.
  5554.  
  5555. <div class="node">
  5556. <p><hr>
  5557. <a name="Why-doesnt-yyrestart()-set-the-start-state-back-to-INITIAL%3f"></a>
  5558. <a name="Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f"></a>
  5559. Next: <a rel="next" accesskey="n" href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>,
  5560. Previous: <a rel="previous" accesskey="p" href="#If-I-have-a-simple-grammar-cant-I-just-parse-it-with-flex_003f">If I have a simple grammar cant I just parse it with flex?</a>,
  5561. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5562.  
  5563. </div>
  5564.  
  5565. <h3 class="unnumberedsec">Why doesn't yyrestart() set the start state back to INITIAL?</h3>
  5566.  
  5567. <p>There are two reasons.  The first is that there might
  5568. be programs that rely on the start state not changing across file changes. 
  5569. The second is that beginning with <code>flex</code> version 2.4, use of <code>yyrestart()</code> is no longer required,
  5570. so fixing the problem there doesn't solve the more general problem.
  5571.  
  5572. <div class="node">
  5573. <p><hr>
  5574. <a name="How-can-I-match-C-style-comments%3f"></a>
  5575. <a name="How-can-I-match-C_002dstyle-comments_003f"></a>
  5576. Next: <a rel="next" accesskey="n" href="#The-period-isnt-working-the-way-I-expected_002e">The period isnt working the way I expected.</a>,
  5577. Previous: <a rel="previous" accesskey="p" href="#Why-doesnt-yyrestart_0028_0029-set-the-start-state-back-to-INITIAL_003f">Why doesnt yyrestart() set the start state back to INITIAL?</a>,
  5578. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5579.  
  5580. </div>
  5581.  
  5582. <h3 class="unnumberedsec">How can I match C-style comments?</h3>
  5583.  
  5584. <p>You might be tempted to try something like this:
  5585.  
  5586. <pre class="example"><pre class="verbatim">     
  5587.      "/*".*"*/"       // WRONG!
  5588. </pre>
  5589. </pre>
  5590.    <p>or, worse, this:
  5591.  
  5592. <pre class="example"><pre class="verbatim">     
  5593.      "/*"(.|\n)"*/"   // WRONG!
  5594. </pre>
  5595. </pre>
  5596.    <p>The above rules will eat too much input, and blow up on things like:
  5597.  
  5598. <pre class="example"><pre class="verbatim">     
  5599.      /* a comment */ do_my_thing( "oops */" );
  5600. </pre>
  5601. </pre>
  5602.    <p>Here is one way which allows you to track line information:
  5603.  
  5604. <pre class="example"><pre class="verbatim">     
  5605.      <INITIAL>{
  5606.      "/*"              BEGIN(IN_COMMENT);
  5607.      }
  5608.      <IN_COMMENT>{
  5609.      "*/"      BEGIN(INITIAL);
  5610.      [^*\n]+   // eat comment in chunks
  5611.      "*"       // eat the lone star
  5612.      \n        yylineno++;
  5613.      }
  5614. </pre>
  5615. </pre>
  5616.    <div class="node">
  5617. <p><hr>
  5618. <a name="The-period-isnt-working-the-way-I-expected."></a>
  5619. <a name="The-period-isnt-working-the-way-I-expected_002e"></a>
  5620. Next: <a rel="next" accesskey="n" href="#Can-I-get-the-flex-manual-in-another-format_003f">Can I get the flex manual in another format?</a>,
  5621. Previous: <a rel="previous" accesskey="p" href="#How-can-I-match-C_002dstyle-comments_003f">How can I match C-style comments?</a>,
  5622. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5623.  
  5624. </div>
  5625.  
  5626. <h3 class="unnumberedsec">The '.' isn't working the way I expected.</h3>
  5627.  
  5628. <p>Here are some tips for using `<samp><span class="samp">.</span></samp>':
  5629.  
  5630.      <ul>
  5631. <li>A common mistake is to place the grouping parenthesis AFTER an operator, when
  5632. you really meant to place the parenthesis BEFORE the operator, e.g., you
  5633. probably want this <code>(foo|bar)+</code> and NOT this <code>(foo|bar+)</code>.
  5634.  
  5635.      <p>The first pattern matches the words `<samp><span class="samp">foo</span></samp>' or `<samp><span class="samp">bar</span></samp>' any number of
  5636. times, e.g., it matches the text `<samp><span class="samp">barfoofoobarfoo</span></samp>'. The
  5637. second pattern matches a single instance of <code>foo</code> or a single instance of
  5638. <code>bar</code> followed by one or more `<samp><span class="samp">r</span></samp>'s, e.g., it matches the text <code>barrrr</code> . 
  5639. <li>A `<samp><span class="samp">.</span></samp>' inside `<samp><span class="samp">[]</span></samp>''s just means a literal`<samp><span class="samp">.</span></samp>' (period),
  5640. and NOT “any character except newline”. 
  5641. <li>Remember that `<samp><span class="samp">.</span></samp>' matches any character EXCEPT `<samp><span class="samp">\n</span></samp>' (and `<samp><span class="samp">EOF</span></samp>'). 
  5642. If you really want to match ANY character, including newlines, then use <code>(.|\n)</code>
  5643. Beware that the regex <code>(.|\n)+</code> will match your entire input! 
  5644. <li>Finally, if you want to match a literal `<samp><span class="samp">.</span></samp>' (a period), then use `<samp><span class="samp">[.]</span></samp>' or `<samp><span class="samp">"."</span></samp>'
  5645. </ul>
  5646.  
  5647. <div class="node">
  5648. <p><hr>
  5649. <a name="Can-I-get-the-flex-manual-in-another-format%3f"></a>
  5650. <a name="Can-I-get-the-flex-manual-in-another-format_003f"></a>
  5651. Next: <a rel="next" accesskey="n" href="#Does-there-exist-a-_0022faster_0022-NDFA_002d_003eDFA-algorithm_003f">Does there exist a "faster" NDFA->DFA algorithm?</a>,
  5652. Previous: <a rel="previous" accesskey="p" href="#The-period-isnt-working-the-way-I-expected_002e">The period isnt working the way I expected.</a>,
  5653. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5654.  
  5655. </div>
  5656.  
  5657. <h3 class="unnumberedsec">Can I get the flex manual in another format?</h3>
  5658.  
  5659. <p>The <code>flex</code> source distribution  includes a texinfo manual. You are
  5660. free to convert that texinfo into whatever format you desire. The
  5661. <code>texinfo</code> package includes tools for conversion to a number of formats.
  5662.  
  5663. <div class="node">
  5664. <p><hr>
  5665. <a name="Does-there-exist-a-%22faster%22-NDFA-%3eDFA-algorithm%3f"></a>
  5666. <a name="Does-there-exist-a-_0022faster_0022-NDFA_002d_003eDFA-algorithm_003f"></a>
  5667. Next: <a rel="next" accesskey="n" href="#How-does-flex-compile-the-DFA-so-quickly_003f">How does flex compile the DFA so quickly?</a>,
  5668. Previous: <a rel="previous" accesskey="p" href="#Can-I-get-the-flex-manual-in-another-format_003f">Can I get the flex manual in another format?</a>,
  5669. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5670.  
  5671. </div>
  5672.  
  5673. <h3 class="unnumberedsec">Does there exist a "faster" NDFA->DFA algorithm?</h3>
  5674.  
  5675. <p>There's no way around the potential exponential running time - it
  5676. can take you exponential time just to enumerate all of the DFA states. 
  5677. In practice, though, the running time is closer to linear, or sometimes
  5678. quadratic.
  5679.  
  5680. <div class="node">
  5681. <p><hr>
  5682. <a name="How-does-flex-compile-the-DFA-so-quickly%3f"></a>
  5683. <a name="How-does-flex-compile-the-DFA-so-quickly_003f"></a>
  5684. Next: <a rel="next" accesskey="n" href="#How-can-I-use-more-than-8192-rules_003f">How can I use more than 8192 rules?</a>,
  5685. Previous: <a rel="previous" accesskey="p" href="#Does-there-exist-a-_0022faster_0022-NDFA_002d_003eDFA-algorithm_003f">Does there exist a "faster" NDFA->DFA algorithm?</a>,
  5686. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5687.  
  5688. </div>
  5689.  
  5690. <h3 class="unnumberedsec">How does flex compile the DFA so quickly?</h3>
  5691.  
  5692. <p>There are two big speed wins that <code>flex</code> uses:
  5693.  
  5694.      <ol type=1 start=1>
  5695. <li>It analyzes the input rules to construct equivalence classes for those
  5696. characters that always make the same transitions.  It then rewrites the NFA
  5697. using equivalence classes for transitions instead of characters.  This cuts
  5698. down the NFA->DFA computation time dramatically, to the point where, for
  5699. uncompressed DFA tables, the DFA generation is often I/O bound in writing out
  5700. the tables. 
  5701. <li>It maintains hash values for previously computed DFA states, so testing
  5702. whether a newly constructed DFA state is equivalent to a previously constructed
  5703. state can be done very quickly, by first comparing hash values.
  5704.         </ol>
  5705.  
  5706. <div class="node">
  5707. <p><hr>
  5708. <a name="How-can-I-use-more-than-8192-rules%3f"></a>
  5709. <a name="How-can-I-use-more-than-8192-rules_003f"></a>
  5710. Next: <a rel="next" accesskey="n" href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>,
  5711. Previous: <a rel="previous" accesskey="p" href="#How-does-flex-compile-the-DFA-so-quickly_003f">How does flex compile the DFA so quickly?</a>,
  5712. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5713.  
  5714. </div>
  5715.  
  5716. <h3 class="unnumberedsec">How can I use more than 8192 rules?</h3>
  5717.  
  5718. <p><code>Flex</code> is compiled with an upper limit of 8192 rules per scanner. 
  5719. If you need more than 8192 rules in your scanner, you'll have to recompile <code>flex</code>
  5720. with the following changes in <samp><span class="file">flexdef.h</span></samp>:
  5721.  
  5722. <pre class="example"><pre class="verbatim">     
  5723.      <    #define YY_TRAILING_MASK 0x2000
  5724.      <    #define YY_TRAILING_HEAD_MASK 0x4000
  5725.      --
  5726.      >    #define YY_TRAILING_MASK 0x20000000
  5727.      >    #define YY_TRAILING_HEAD_MASK 0x40000000
  5728. </pre>
  5729. </pre>
  5730.    <p>This should work okay as long as your C compiler uses 32 bit integers. 
  5731. But you might want to think about whether using such a huge number of rules
  5732. is the best way to solve your problem.
  5733.  
  5734.    <p>The following may also be relevant:
  5735.  
  5736.    <p>With luck, you should be able to increase the definitions in flexdef.h for:
  5737.  
  5738. <pre class="example"><pre class="verbatim">     
  5739.      #define JAMSTATE -32766 /* marks a reference to the state that always jams */
  5740.      #define MAXIMUM_MNS 31999
  5741.      #define BAD_SUBSCRIPT -32767
  5742. </pre>
  5743. </pre>
  5744.    <p>recompile everything, and it'll all work.  Flex only has these 16-bit-like
  5745. values built into it because a long time ago it was developed on a machine
  5746. with 16-bit ints.  I've given this advice to others in the past but haven't
  5747. heard back from them whether it worked okay or not...
  5748.  
  5749. <div class="node">
  5750. <p><hr>
  5751. <a name="How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file%3f"></a>
  5752. <a name="How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f"></a>
  5753. Next: <a rel="next" accesskey="n" href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>,
  5754. Previous: <a rel="previous" accesskey="p" href="#How-can-I-use-more-than-8192-rules_003f">How can I use more than 8192 rules?</a>,
  5755. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5756.  
  5757. </div>
  5758.  
  5759. <h3 class="unnumberedsec">How do I abandon a file in the middle of a scan and switch to a new file?</h3>
  5760.  
  5761. <p>Just call <code>yyrestart(newfile)</code>. Be sure to reset the start state if you want a
  5762. “fresh start, since <code>yyrestart</code> does NOT reset the start state back to <code>INITIAL</code>.
  5763.  
  5764. <div class="node">
  5765. <p><hr>
  5766. <a name="How-do-I-execute-code-only-during-initialization-(only-before-the-first-scan)%3f"></a>
  5767. <a name="How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f"></a>
  5768. Next: <a rel="next" accesskey="n" href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>,
  5769. Previous: <a rel="previous" accesskey="p" href="#How-do-I-abandon-a-file-in-the-middle-of-a-scan-and-switch-to-a-new-file_003f">How do I abandon a file in the middle of a scan and switch to a new file?</a>,
  5770. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5771.  
  5772. </div>
  5773.  
  5774. <h3 class="unnumberedsec">How do I execute code only during initialization (only before the first scan)?</h3>
  5775.  
  5776. <p>You can specify an initial action by defining the macro <code>YY_USER_INIT</code> (though
  5777. note that <code>yyout</code> may not be available at the time this macro is executed).  Or you
  5778. can add to the beginning of your rules section:
  5779.  
  5780. <pre class="example"><pre class="verbatim">     
  5781.      %%
  5782.      /* Must be indented! */
  5783.      static int did_init = 0;
  5784.      
  5785.      if ( ! did_init ){
  5786.      do_my_init();
  5787.      did_init = 1;
  5788.      }
  5789. </pre>
  5790. </pre>
  5791.    <div class="node">
  5792. <p><hr>
  5793. <a name="How-do-I-execute-code-at-termination%3f"></a>
  5794. <a name="How-do-I-execute-code-at-termination_003f"></a>
  5795. Next: <a rel="next" accesskey="n" href="#Where-else-can-I-find-help_003f">Where else can I find help?</a>,
  5796. Previous: <a rel="previous" accesskey="p" href="#How-do-I-execute-code-only-during-initialization-_0028only-before-the-first-scan_0029_003f">How do I execute code only during initialization (only before the first scan)?</a>,
  5797. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5798.  
  5799. </div>
  5800.  
  5801. <h3 class="unnumberedsec">How do I execute code at termination?</h3>
  5802.  
  5803. <p>You can specify an action for the <code><<EOF>></code> rule.
  5804.  
  5805. <div class="node">
  5806. <p><hr>
  5807. <a name="Where-else-can-I-find-help%3f"></a>
  5808. <a name="Where-else-can-I-find-help_003f"></a>
  5809. Next: <a rel="next" accesskey="n" href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>,
  5810. Previous: <a rel="previous" accesskey="p" href="#How-do-I-execute-code-at-termination_003f">How do I execute code at termination?</a>,
  5811. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5812.  
  5813. </div>
  5814.  
  5815. <h3 class="unnumberedsec">Where else can I find help?</h3>
  5816.  
  5817. <p>You can find the flex homepage on the web at
  5818. <a href="http://lex.sourceforge.net/">http://lex.sourceforge.net/</a>. See that page for details about flex
  5819. mailing lists as well.
  5820.  
  5821. <div class="node">
  5822. <p><hr>
  5823. <a name="Can-I-include-comments-in-the-%22rules%22-section-of-the-file%3f"></a>
  5824. <a name="Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f"></a>
  5825. Next: <a rel="next" accesskey="n" href="#I-get-an-error-about-undefined-yywrap_0028_0029_002e">I get an error about undefined yywrap().</a>,
  5826. Previous: <a rel="previous" accesskey="p" href="#Where-else-can-I-find-help_003f">Where else can I find help?</a>,
  5827. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5828.  
  5829. </div>
  5830.  
  5831. <h3 class="unnumberedsec">Can I include comments in the "rules" section of the file?</h3>
  5832.  
  5833. <p>Yes, just about anywhere you want to. See the manual for the specific syntax.
  5834.  
  5835. <div class="node">
  5836. <p><hr>
  5837. <a name="I-get-an-error-about-undefined-yywrap()."></a>
  5838. <a name="I-get-an-error-about-undefined-yywrap_0028_0029_002e"></a>
  5839. Next: <a rel="next" accesskey="n" href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>,
  5840. Previous: <a rel="previous" accesskey="p" href="#Can-I-include-comments-in-the-_0022rules_0022-section-of-the-file_003f">Can I include comments in the "rules" section of the file?</a>,
  5841. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5842.  
  5843. </div>
  5844.  
  5845. <h3 class="unnumberedsec">I get an error about undefined yywrap().</h3>
  5846.  
  5847. <p>You must supply a <code>yywrap()</code> function of your own, or link to <samp><span class="file">libfl.a</span></samp>
  5848. (which provides one), or use
  5849.  
  5850. <pre class="example"><pre class="verbatim">     
  5851.      %option noyywrap
  5852. </pre>
  5853. </pre>
  5854.    <p>in your source to say you don't want a <code>yywrap()</code> function.
  5855.  
  5856. <div class="node">
  5857. <p><hr>
  5858. <a name="How-can-I-change-the-matching-pattern-at-run-time%3f"></a>
  5859. <a name="How-can-I-change-the-matching-pattern-at-run-time_003f"></a>
  5860. Next: <a rel="next" accesskey="n" href="#How-can-I-expand-macros-in-the-input_003f">How can I expand macros in the input?</a>,
  5861. Previous: <a rel="previous" accesskey="p" href="#I-get-an-error-about-undefined-yywrap_0028_0029_002e">I get an error about undefined yywrap().</a>,
  5862. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5863.  
  5864. </div>
  5865.  
  5866. <h3 class="unnumberedsec">How can I change the matching pattern at run time?</h3>
  5867.  
  5868. <p>You can't, it's compiled into a static table when flex builds the scanner.
  5869.  
  5870. <div class="node">
  5871. <p><hr>
  5872. <a name="How-can-I-expand-macros-in-the-input%3f"></a>
  5873. <a name="How-can-I-expand-macros-in-the-input_003f"></a>
  5874. Next: <a rel="next" accesskey="n" href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>,
  5875. Previous: <a rel="previous" accesskey="p" href="#How-can-I-change-the-matching-pattern-at-run-time_003f">How can I change the matching pattern at run time?</a>,
  5876. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5877.  
  5878. </div>
  5879.  
  5880. <h3 class="unnumberedsec">How can I expand macros in the input?</h3>
  5881.  
  5882. <p>The best way to approach this problem is at a higher level, e.g., in the parser.
  5883.  
  5884.    <p>However, you can do this using multiple input buffers.
  5885.  
  5886. <pre class="example"><pre class="verbatim">     
  5887.      %%
  5888.      macro/[a-z]+    {
  5889.      /* Saw the macro "macro" followed by extra stuff. */
  5890.      main_buffer = YY_CURRENT_BUFFER;
  5891.      expansion_buffer = yy_scan_string(expand(yytext));
  5892.      yy_switch_to_buffer(expansion_buffer);
  5893.      }
  5894.      
  5895.      <<EOF>>    {
  5896.      if ( expansion_buffer )
  5897.      {
  5898.      // We were doing an expansion, return to where
  5899.      // we were.
  5900.      yy_switch_to_buffer(main_buffer);
  5901.      yy_delete_buffer(expansion_buffer);
  5902.      expansion_buffer = 0;
  5903.      }
  5904.      else
  5905.      yyterminate();
  5906.      }
  5907. </pre>
  5908. </pre>
  5909.    <p>You probably will want a stack of expansion buffers to allow nested macros. 
  5910. From the above though hopefully the idea is clear.
  5911.  
  5912. <div class="node">
  5913. <p><hr>
  5914. <a name="How-can-I-build-a-two-pass-scanner%3f"></a>
  5915. <a name="How-can-I-build-a-two_002dpass-scanner_003f"></a>
  5916. Next: <a rel="next" accesskey="n" href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>,
  5917. Previous: <a rel="previous" accesskey="p" href="#How-can-I-expand-macros-in-the-input_003f">How can I expand macros in the input?</a>,
  5918. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5919.  
  5920. </div>
  5921.  
  5922. <h3 class="unnumberedsec">How can I build a two-pass scanner?</h3>
  5923.  
  5924. <p>One way to do it is to filter the first pass to a temporary file,
  5925. then process the temporary file on the second pass. You will probably see a
  5926. performance hit, do to all the disk I/O.
  5927.  
  5928.    <p>When you need to look ahead far forward like this, it almost always means
  5929. that the right solution is to build a parse tree of the entire input, then
  5930. walk it after the parse in order to generate the output.  In a sense, this
  5931. is a two-pass approach, once through the text and once through the parse
  5932. tree, but the performance hit for the latter is usually an order of magnitude
  5933. smaller, since everything is already classified, in binary format, and
  5934. residing in memory.
  5935.  
  5936. <div class="node">
  5937. <p><hr>
  5938. <a name="How-do-I-match-any-string-not-matched-in-the-preceding-rules%3f"></a>
  5939. <a name="How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f"></a>
  5940. Next: <a rel="next" accesskey="n" href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>,
  5941. Previous: <a rel="previous" accesskey="p" href="#How-can-I-build-a-two_002dpass-scanner_003f">How can I build a two-pass scanner?</a>,
  5942. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5943.  
  5944. </div>
  5945.  
  5946. <h3 class="unnumberedsec">How do I match any string not matched in the preceding rules?</h3>
  5947.  
  5948. <p>One way to assign precedence, is to place the more specific rules first. If
  5949. two rules would match the same input (same sequence of characters) then the
  5950. first rule listed in the <code>flex</code> input wins. e.g.,
  5951.  
  5952. <pre class="example"><pre class="verbatim">     
  5953.      %%
  5954.      foo[a-zA-Z_]+    return FOO_ID;
  5955.      bar[a-zA-Z_]+    return BAR_ID;
  5956.      [a-zA-Z_]+       return GENERIC_ID;
  5957. </pre>
  5958. </pre>
  5959.    <p>Note that the rule <code>[a-zA-Z_]+</code> must come *after* the others.  It will match the
  5960. same amount of text as the more specific rules, and in that case the
  5961. <code>flex</code> scanner will pick the first rule listed in your scanner as the
  5962. one to match.
  5963.  
  5964. <div class="node">
  5965. <p><hr>
  5966. <a name="I-am-trying-to-port-code-from-AT%26T-lex-that-uses-yysptr-and-yysbuf."></a>
  5967. <a name="I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e"></a>
  5968. Next: <a rel="next" accesskey="n" href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>,
  5969. Previous: <a rel="previous" accesskey="p" href="#How-do-I-match-any-string-not-matched-in-the-preceding-rules_003f">How do I match any string not matched in the preceding rules?</a>,
  5970. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5971.  
  5972. </div>
  5973.  
  5974. <h3 class="unnumberedsec">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</h3>
  5975.  
  5976. <p>Those are internal variables pointing into the AT&T scanner's input buffer.  I
  5977. imagine they're being manipulated in user versions of the <code>input()</code> and <code>unput()</code>
  5978. functions.  If so, what you need to do is analyze those functions to figure out
  5979. what they're doing, and then replace <code>input()</code> with an appropriate definition of
  5980. <code>YY_INPUT</code>.  You shouldn't need to (and must not) replace
  5981. <code>flex</code>'s <code>unput()</code> function.
  5982.  
  5983. <div class="node">
  5984. <p><hr>
  5985. <a name="Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character%3f"></a>
  5986. <a name="Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f"></a>
  5987. Next: <a rel="next" accesskey="n" href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>,
  5988. Previous: <a rel="previous" accesskey="p" href="#I-am-trying-to-port-code-from-AT_0026T-lex-that-uses-yysptr-and-yysbuf_002e">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</a>,
  5989. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  5990.  
  5991. </div>
  5992.  
  5993. <h3 class="unnumberedsec">Is there a way to make flex treat NULL like a regular character?</h3>
  5994.  
  5995. <p>Yes, `<samp><span class="samp">\0</span></samp>' and `<samp><span class="samp">\x00</span></samp>' should both do the trick.  Perhaps you have an ancient
  5996. version of <code>flex</code>.  The latest release is version 2.5.31.
  5997.  
  5998. <div class="node">
  5999. <p><hr>
  6000. <a name="Whenever-flex-can-not-match-the-input-it-says-%22flex-scanner-jammed%22."></a>
  6001. <a name="Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e"></a>
  6002. Next: <a rel="next" accesskey="n" href="#Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f">Why doesnt flex have non-greedy operators like perl does?</a>,
  6003. Previous: <a rel="previous" accesskey="p" href="#Is-there-a-way-to-make-flex-treat-NULL-like-a-regular-character_003f">Is there a way to make flex treat NULL like a regular character?</a>,
  6004. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6005.  
  6006. </div>
  6007.  
  6008. <h3 class="unnumberedsec">Whenever flex can not match the input it says "flex scanner jammed".</h3>
  6009.  
  6010. <p>You need to add a rule that matches the otherwise-unmatched text. 
  6011. e.g.,
  6012.  
  6013. <pre class="example"><pre class="verbatim">     
  6014.      %option yylineno
  6015.      %%
  6016.      [[a bunch of rules here]]
  6017.      
  6018.      .    printf("bad input character '%s' at line %d\n", yytext, yylineno);
  6019. </pre>
  6020. </pre>
  6021.    <p>See <code>%option default</code> for more information.
  6022.  
  6023. <div class="node">
  6024. <p><hr>
  6025. <a name="Why-doesnt-flex-have-non-greedy-operators-like-perl-does%3f"></a>
  6026. <a name="Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f"></a>
  6027. Next: <a rel="next" accesskey="n" href="#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e">Memory leak - 16386 bytes allocated by malloc.</a>,
  6028. Previous: <a rel="previous" accesskey="p" href="#Whenever-flex-can-not-match-the-input-it-says-_0022flex-scanner-jammed_0022_002e">Whenever flex can not match the input it says "flex scanner jammed".</a>,
  6029. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6030.  
  6031. </div>
  6032.  
  6033. <h3 class="unnumberedsec">Why doesn't flex have non-greedy operators like perl does?</h3>
  6034.  
  6035. <p>A DFA can do a non-greedy match by stopping
  6036. the first time it enters an accepting state, instead of consuming input until
  6037. it determines that no further matching is possible (a “jam” state).  This
  6038. is actually easier to implement than longest leftmost match (which flex does).
  6039.  
  6040.    <p>But it's also much less useful than longest leftmost match.  In general,
  6041. when you find yourself wishing for non-greedy matching, that's usually a
  6042. sign that you're trying to make the scanner do some parsing.  That's
  6043. generally the wrong approach, since it lacks the power to do a decent job. 
  6044. Better is to either introduce a separate parser, or to split the scanner
  6045. into multiple scanners using (exclusive) start conditions.
  6046.  
  6047.    <p>You might have
  6048. a separate start state once you've seen the `<samp><span class="samp">BEGIN</span></samp>'. In that state, you
  6049. might then have a regex that will match `<samp><span class="samp">END</span></samp>' (to kick you out of the
  6050. state), and perhaps `<samp><span class="samp">(.|\n)</span></samp>' to get a single character within the chunk ...
  6051.  
  6052.    <p>This approach also has much better error-reporting properties.
  6053.  
  6054. <div class="node">
  6055. <p><hr>
  6056. <a name="Memory-leak---16386-bytes-allocated-by-malloc."></a>
  6057. <a name="Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e"></a>
  6058. Next: <a rel="next" accesskey="n" href="#How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f">How do I track the byte offset for lseek()?</a>,
  6059. Previous: <a rel="previous" accesskey="p" href="#Why-doesnt-flex-have-non_002dgreedy-operators-like-perl-does_003f">Why doesnt flex have non-greedy operators like perl does?</a>,
  6060. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6061.  
  6062. </div>
  6063.  
  6064. <h3 class="unnumberedsec">Memory leak - 16386 bytes allocated by malloc.</h3>
  6065.  
  6066. <p><a name="faq_002dmemory_002dleak"></a>
  6067.  
  6068.    <p>UPDATED 2002-07-10: As of <code>flex</code> version 2.5.9, this leak means that you did not
  6069. call <code>yylex_destroy()</code>. If you are using an earlier version of <code>flex</code>, then read
  6070. on.
  6071.  
  6072.    <p>The leak is about 16426 bytes.  That is, (8192 * 2 + 2) for the read-buffer, and
  6073. about 40 for <code>struct yy_buffer_state</code> (depending upon alignment). The leak is in
  6074. the non-reentrant C scanner only (NOT in the reentrant scanner, NOT in the C++
  6075. scanner). Since <code>flex</code> doesn't know when you are done, the buffer is never freed.
  6076.  
  6077.    <p>However, the leak won't multiply since the buffer is reused no matter how many
  6078. times you call <code>yylex()</code>.
  6079.  
  6080.    <p>If you want to reclaim the memory when you are completely done scanning, then
  6081. you might try this:
  6082.  
  6083. <pre class="example"><pre class="verbatim">     
  6084.      /* For non-reentrant C scanner only. */
  6085.      yy_delete_buffer(YY_CURRENT_BUFFER);
  6086.      yy_init = 1;
  6087. </pre>
  6088. </pre>
  6089.    <p>Note: <code>yy_init</code> is an "internal variable", and hasn't been tested in this
  6090. situation. It is possible that some other globals may need resetting as well.
  6091.  
  6092. <div class="node">
  6093. <p><hr>
  6094. <a name="How-do-I-track-the-byte-offset-for-lseek()%3f"></a>
  6095. <a name="How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f"></a>
  6096. Next: <a rel="next" accesskey="n" href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>,
  6097. Previous: <a rel="previous" accesskey="p" href="#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e">Memory leak - 16386 bytes allocated by malloc.</a>,
  6098. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6099.  
  6100. </div>
  6101.  
  6102. <h3 class="unnumberedsec">How do I track the byte offset for lseek()?</h3>
  6103.  
  6104. <pre class="example"><pre class="verbatim">     
  6105.      >   We thought that it would be possible to have this number through the
  6106.      >   evaluation of the following expression:
  6107.      >
  6108.      >   seek_position = (no_buffers)*YY_READ_BUF_SIZE + yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf
  6109. </pre>
  6110. </pre>
  6111.    <p>While this is the right idea, it has two problems.  The first is that
  6112. it's possible that <code>flex</code> will request less than <code>YY_READ_BUF_SIZE</code> during
  6113. an invocation of <code>YY_INPUT</code> (or that your input source will return less
  6114. even though <code>YY_READ_BUF_SIZE</code> bytes were requested).  The second problem
  6115. is that when refilling its internal buffer, <code>flex</code> keeps some characters
  6116. from the previous buffer (because usually it's in the middle of a match,
  6117. and needs those characters to construct <code>yytext</code> for the match once it's
  6118. done).  Because of this, <code>yy_c_buf_p - YY_CURRENT_BUFFER->yy_ch_buf</code> won't
  6119. be exactly the number of characters already read from the current buffer.
  6120.  
  6121.    <p>An alternative solution is to count the number of characters you've matched
  6122. since starting to scan.  This can be done by using <code>YY_USER_ACTION</code>.  For
  6123. example,
  6124.  
  6125. <pre class="example"><pre class="verbatim">     
  6126.      #define YY_USER_ACTION num_chars += yyleng;
  6127. </pre>
  6128. </pre>
  6129.    <p>(You need to be careful to update your bookkeeping if you use <code>yymore(</code>),
  6130. <code>yyless()</code>, <code>unput()</code>, or <code>input()</code>.)
  6131.  
  6132. <div class="node">
  6133. <p><hr>
  6134. <a name="How-do-I-use-my-own-I%2fO-classes-in-a-C++-scanner%3f"></a>
  6135. <a name="How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f"></a>
  6136. Next: <a rel="next" accesskey="n" href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>,
  6137. Previous: <a rel="previous" accesskey="p" href="#How-do-I-track-the-byte-offset-for-lseek_0028_0029_003f">How do I track the byte offset for lseek()?</a>,
  6138. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6139.  
  6140. </div>
  6141.  
  6142. <h3 class="section">How do I use my own I/O classes in a C++ scanner?</h3>
  6143.  
  6144. <p>When the flex C++ scanning class rewrite finally happens, then this sort of thing should become much easier.
  6145.  
  6146.    <p><a name="index-LexerOutput_002c-overriding-481"></a><a name="index-LexerInput_002c-overriding-482"></a><a name="index-overriding-LexerOutput-483"></a><a name="index-overriding-LexerInput-484"></a><a name="index-customizing-I_002fO-in-C_002b_002b-scanners-485"></a><a name="index-C_002b_002b-I_002fO_002c-customizing-486"></a>You can do this by passing the various functions (such as <code>LexerInput()</code>
  6147. and <code>LexerOutput()</code>) NULL <code>iostream*</code>'s, and then
  6148. dealing with your own I/O classes surreptitiously (i.e., stashing them in
  6149. special member variables).  This works because the only assumption about
  6150. the lexer regarding what's done with the iostream's is that they're
  6151. ultimately passed to <code>LexerInput()</code> and <code>LexerOutput</code>, which then do whatever
  6152. is necessary with them.
  6153.  
  6154. <!-- faq edit stopped here -->
  6155. <div class="node">
  6156. <p><hr>
  6157. <a name="How-do-I-skip-as-many-chars-as-possible%3f"></a>
  6158. <a name="How-do-I-skip-as-many-chars-as-possible_003f"></a>
  6159. Next: <a rel="next" accesskey="n" href="#deleteme00">deleteme00</a>,
  6160. Previous: <a rel="previous" accesskey="p" href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a>,
  6161. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6162.  
  6163. </div>
  6164.  
  6165. <h3 class="unnumberedsec">How do I skip as many chars as possible?</h3>
  6166.  
  6167. <p>How do I skip as many chars as possible – without interfering with the other
  6168. patterns?
  6169.  
  6170.    <p>In the example below, we want to skip over characters until we see the phrase
  6171. "endskip". The following will <em>NOT</em> work correctly (do you see why not?)
  6172.  
  6173. <pre class="example"><pre class="verbatim">     
  6174.      /* INCORRECT SCANNER */
  6175.      %x SKIP
  6176.      %%
  6177.      <INITIAL>startskip   BEGIN(SKIP);
  6178.      ...
  6179.      <SKIP>"endskip"       BEGIN(INITIAL);
  6180.      <SKIP>.*             ;
  6181. </pre>
  6182. </pre>
  6183.    <p>The problem is that the pattern .* will eat up the word "endskip." 
  6184. The simplest (but slow) fix is:
  6185.  
  6186. <pre class="example"><pre class="verbatim">     
  6187.      <SKIP>"endskip"      BEGIN(INITIAL);
  6188.      <SKIP>.              ;
  6189. </pre>
  6190. </pre>
  6191.    <p>The fix involves making the second rule match more, without
  6192. making it match "endskip" plus something else.  So for example:
  6193.  
  6194. <pre class="example"><pre class="verbatim">     
  6195.      <SKIP>"endskip"     BEGIN(INITIAL);
  6196.      <SKIP>[^e]+         ;
  6197.      <SKIP>.                ;/* so you eat up e's, too */
  6198. </pre>
  6199. </pre>
  6200.    <!-- TODO: Evaluate this faq. -->
  6201. <div class="node">
  6202. <p><hr>
  6203. <a name="deleteme00"></a>
  6204. Next: <a rel="next" accesskey="n" href="#Are-certain-equivalent-patterns-faster-than-others_003f">Are certain equivalent patterns faster than others?</a>,
  6205. Previous: <a rel="previous" accesskey="p" href="#How-do-I-skip-as-many-chars-as-possible_003f">How do I skip as many chars as possible?</a>,
  6206. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6207.  
  6208. </div>
  6209.  
  6210. <h3 class="unnumberedsec">deleteme00</h3>
  6211.  
  6212. <pre class="example"><pre class="verbatim">     
  6213.      QUESTION:
  6214.      When was flex born?
  6215.      
  6216.      Vern Paxson took over
  6217.      the Software Tools lex project from Jef Poskanzer in 1982.  At that point it
  6218.      was written in Ratfor.  Around 1987 or so, Paxson translated it into C, and
  6219.      a legend was born :-).
  6220. </pre>
  6221. </pre>
  6222. <!-- TODO: Evaluate this faq. -->
  6223. <div class="node">
  6224. <p><hr>
  6225. <a name="Are-certain-equivalent-patterns-faster-than-others%3f"></a>
  6226. <a name="Are-certain-equivalent-patterns-faster-than-others_003f"></a>
  6227. Next: <a rel="next" accesskey="n" href="#Is-backing-up-a-big-deal_003f">Is backing up a big deal?</a>,
  6228. Previous: <a rel="previous" accesskey="p" href="#deleteme00">deleteme00</a>,
  6229. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6230.  
  6231. </div>
  6232.  
  6233. <h3 class="unnumberedsec">Are certain equivalent patterns faster than others?</h3>
  6234.  
  6235. <pre class="example"><pre class="verbatim">     
  6236.      To: Adoram Rogel <adoram@orna.hybridge.com>
  6237.      Subject: Re: Flex 2.5.2 performance questions
  6238.      In-reply-to: Your message of Wed, 18 Sep 96 11:12:17 EDT.
  6239.      Date: Wed, 18 Sep 96 10:51:02 PDT
  6240.      From: Vern Paxson <vern>
  6241.      
  6242.      [Note, the most recent flex release is 2.5.4, which you can get from
  6243.      ftp.ee.lbl.gov.  It has bug fixes over 2.5.2 and 2.5.3.]
  6244.      
  6245.      > 1. Using the pattern
  6246.      >    ([Ff](oot)?)?[Nn](ote)?(\.)?
  6247.      >    instead of
  6248.      >    (((F|f)oot(N|n)ote)|((N|n)ote)|((N|n)\.)|((F|f)(N|n)(\.)))
  6249.      >    (in a very complicated flex program) caused the program to slow from
  6250.      >    300K+/min to 100K/min (no other changes were done).
  6251.      
  6252.      These two are not equivalent.  For example, the first can match "footnote."
  6253.      but the second can only match "footnote".  This is almost certainly the
  6254.      cause in the discrepancy - the slower scanner run is matching more tokens,
  6255.      and/or having to do more backing up.
  6256.      
  6257.      > 2. Which of these two are better: [Ff]oot or (F|f)oot ?
  6258.      
  6259.      From a performance point of view, they're equivalent (modulo presumably
  6260.      minor effects such as memory cache hit rates; and the presence of trailing
  6261.      context, see below).  From a space point of view, the first is slightly
  6262.      preferable.
  6263.      
  6264.      > 3. I have a pattern that look like this:
  6265.      >    pats {p1}|{p2}|{p3}|...|{p50}     (50 patterns ORd)
  6266.      >
  6267.      >    running yet another complicated program that includes the following rule:
  6268.      >    <snext>{and}/{no4}{bb}{pats}
  6269.      >
  6270.      >    gets me to "too complicated - over 32,000 states"...
  6271.      
  6272.      I can't tell from this example whether the trailing context is variable-length
  6273.      or fixed-length (it could be the latter if {and} is fixed-length).  If it's
  6274.      variable length, which flex -p will tell you, then this reflects a basic
  6275.      performance problem, and if you can eliminate it by restructuring your
  6276.      scanner, you will see significant improvement.
  6277.      
  6278.      >    so I divided {pats} to {pats1}, {pats2},..., {pats5} each consists of about
  6279.      >    10 patterns and changed the rule to be 5 rules.
  6280.      >    This did compile, but what is the rule of thumb here ?
  6281.      
  6282.      The rule is to avoid trailing context other than fixed-length, in which for
  6283.      a/b, either the 'a' pattern or the 'b' pattern have a fixed length.  Use
  6284.      of the '|' operator automatically makes the pattern variable length, so in
  6285.      this case '[Ff]oot' is preferred to '(F|f)oot'.
  6286.      
  6287.      > 4. I changed a rule that looked like this:
  6288.      >    <snext8>{and}{bb}/{ROMAN}[^A-Za-z] { BEGIN...
  6289.      >
  6290.      >    to the next 2 rules:
  6291.      >    <snext8>{and}{bb}/{ROMAN}[A-Za-z] { ECHO;}
  6292.      >    <snext8>{and}{bb}/{ROMAN}         { BEGIN...
  6293.      >
  6294.      >    Again, I understand the using [^...] will cause a great performance loss
  6295.      
  6296.      Actually, it doesn't cause any sort of performance loss.  It's a surprising
  6297.      fact about regular expressions that they always match in linear time
  6298.      regardless of how complex they are.
  6299.      
  6300.      >    but are there any specific rules about it ?
  6301.      
  6302.      See the "Performance Considerations" section of the man page, and also
  6303.      the example in MISC/fastwc/.
  6304.      
  6305.              Vern
  6306. </pre>
  6307. </pre>
  6308. <!-- TODO: Evaluate this faq. -->
  6309. <div class="node">
  6310. <p><hr>
  6311. <a name="Is-backing-up-a-big-deal%3f"></a>
  6312. <a name="Is-backing-up-a-big-deal_003f"></a>
  6313. Next: <a rel="next" accesskey="n" href="#Can-I-fake-multi_002dbyte-character-support_003f">Can I fake multi-byte character support?</a>,
  6314. Previous: <a rel="previous" accesskey="p" href="#Are-certain-equivalent-patterns-faster-than-others_003f">Are certain equivalent patterns faster than others?</a>,
  6315. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6316.  
  6317. </div>
  6318.  
  6319. <h3 class="unnumberedsec">Is backing up a big deal?</h3>
  6320.  
  6321. <pre class="example"><pre class="verbatim">     
  6322.      To: Adoram Rogel <adoram@hybridge.com>
  6323.      Subject: Re: Flex 2.5.2 performance questions
  6324.      In-reply-to: Your message of Thu, 19 Sep 96 10:16:04 EDT.
  6325.      Date: Thu, 19 Sep 96 09:58:00 PDT
  6326.      From: Vern Paxson <vern>
  6327.      
  6328.      > a lot about the backing up problem.
  6329.      > I believe that there lies my biggest problem, and I'll try to improve
  6330.      > it.
  6331.      
  6332.      Since you have variable trailing context, this is a bigger performance
  6333.      problem.  Fixing it is usually easier than fixing backing up, which in a
  6334.      complicated scanner (yours seems to fit the bill) can be extremely
  6335.      difficult to do correctly.
  6336.      
  6337.      You also don't mention what flags you are using for your scanner.
  6338.      -f makes a large speed difference, and -Cfe buys you nearly as much
  6339.      speed but the resulting scanner is considerably smaller.
  6340.      
  6341.      > I have an | operator in {and} and in {pats} so both of them are variable
  6342.      > length.
  6343.      
  6344.      -p should have reported this.
  6345.      
  6346.      > Is changing one of them to fixed-length is enough ?
  6347.      
  6348.      Yes.
  6349.      
  6350.      > Is it possible to change the 32,000 states limit ?
  6351.      
  6352.      Yes.  I've appended instructions on how.  Before you make this change,
  6353.      though, you should think about whether there are ways to fundamentally
  6354.      simplify your scanner - those are certainly preferable!
  6355.      
  6356.              Vern
  6357.      
  6358.      To increase the 32K limit (on a machine with 32 bit integers), you increase
  6359.      the magnitude of the following in flexdef.h:
  6360.      
  6361.      #define JAMSTATE -32766 /* marks a reference to the state that always jams */
  6362.      #define MAXIMUM_MNS 31999
  6363.      #define BAD_SUBSCRIPT -32767
  6364.      #define MAX_SHORT 32700
  6365.      
  6366.      Adding a 0 or two after each should do the trick.
  6367. </pre>
  6368. </pre>
  6369. <!-- TODO: Evaluate this faq. -->
  6370. <div class="node">
  6371. <p><hr>
  6372. <a name="Can-I-fake-multi-byte-character-support%3f"></a>
  6373. <a name="Can-I-fake-multi_002dbyte-character-support_003f"></a>
  6374. Next: <a rel="next" accesskey="n" href="#deleteme01">deleteme01</a>,
  6375. Previous: <a rel="previous" accesskey="p" href="#Is-backing-up-a-big-deal_003f">Is backing up a big deal?</a>,
  6376. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6377.  
  6378. </div>
  6379.  
  6380. <h3 class="unnumberedsec">Can I fake multi-byte character support?</h3>
  6381.  
  6382. <pre class="example"><pre class="verbatim">     
  6383.      To: Heeman_Lee@hp.com
  6384.      Subject: Re: flex - multi-byte support?
  6385.      In-reply-to: Your message of Thu, 03 Oct 1996 17:24:04 PDT.
  6386.      Date: Fri, 04 Oct 1996 11:42:18 PDT
  6387.      From: Vern Paxson <vern>
  6388.      
  6389.      >      I assume as long as my *.l file defines the
  6390.      >      range of expected character code values (in octal format), flex will
  6391.      >      scan the file and read multi-byte characters correctly. But I have no
  6392.      >      confidence in this assumption.
  6393.      
  6394.      Your lack of confidence is justified - this won't work.
  6395.      
  6396.      Flex has in it a widespread assumption that the input is processed
  6397.      one byte at a time.  Fixing this is on the to-do list, but is involved,
  6398.      so it won't happen any time soon.  In the interim, the best I can suggest
  6399.      (unless you want to try fixing it yourself) is to write your rules in
  6400.      terms of pairs of bytes, using definitions in the first section:
  6401.      
  6402.          X    \xfe\xc2
  6403.          ...
  6404.          %%
  6405.          foo{X}bar    found_foo_fe_c2_bar();
  6406.      
  6407.      etc.  Definitely a pain - sorry about that.
  6408.      
  6409.      By the way, the email address you used for me is ancient, indicating you
  6410.      have a very old version of flex.  You can get the most recent, 2.5.4, from
  6411.      ftp.ee.lbl.gov.
  6412.      
  6413.              Vern
  6414. </pre>
  6415. </pre>
  6416. <!-- TODO: Evaluate this faq. -->
  6417. <div class="node">
  6418. <p><hr>
  6419. <a name="deleteme01"></a>
  6420. Next: <a rel="next" accesskey="n" href="#Can-you-discuss-some-flex-internals_003f">Can you discuss some flex internals?</a>,
  6421. Previous: <a rel="previous" accesskey="p" href="#Can-I-fake-multi_002dbyte-character-support_003f">Can I fake multi-byte character support?</a>,
  6422. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6423.  
  6424. </div>
  6425.  
  6426. <h3 class="unnumberedsec">deleteme01</h3>
  6427.  
  6428. <pre class="example"><pre class="verbatim">     
  6429.      To: moleary@primus.com
  6430.      Subject: Re: Flex / Unicode compatibility question
  6431.      In-reply-to: Your message of Tue, 22 Oct 1996 10:15:42 PDT.
  6432.      Date: Tue, 22 Oct 1996 11:06:13 PDT
  6433.      From: Vern Paxson <vern>
  6434.      
  6435.      Unfortunately flex at the moment has a widespread assumption within it
  6436.      that characters are processed 8 bits at a time.  I don't see any easy
  6437.      fix for this (other than writing your rules in terms of double characters -
  6438.      a pain).  I also don't know of a wider lex, though you might try surfing
  6439.      the Plan 9 stuff because I know it's a Unicode system, and also the PCCT
  6440.      toolkit (try searching say Alta Vista for "Purdue Compiler Construction
  6441.      Toolkit").
  6442.      
  6443.      Fixing flex to handle wider characters is on the long-term to-do list.
  6444.      But since flex is a strictly spare-time project these days, this probably
  6445.      won't happen for quite a while, unless someone else does it first.
  6446.      
  6447.              Vern
  6448. </pre>
  6449. </pre>
  6450. <!-- TODO: Evaluate this faq. -->
  6451. <div class="node">
  6452. <p><hr>
  6453. <a name="Can-you-discuss-some-flex-internals%3f"></a>
  6454. <a name="Can-you-discuss-some-flex-internals_003f"></a>
  6455. Next: <a rel="next" accesskey="n" href="#unput_0028_0029-messes-up-yy_005fat_005fbol">unput() messes up yy_at_bol</a>,
  6456. Previous: <a rel="previous" accesskey="p" href="#deleteme01">deleteme01</a>,
  6457. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6458.  
  6459. </div>
  6460.  
  6461. <h3 class="unnumberedsec">Can you discuss some flex internals?</h3>
  6462.  
  6463. <pre class="example"><pre class="verbatim">     
  6464.      To: Johan Linde <jl@theophys.kth.se>
  6465.      Subject: Re: translation of flex
  6466.      In-reply-to: Your message of Sun, 10 Nov 1996 09:16:36 PST.
  6467.      Date: Mon, 11 Nov 1996 10:33:50 PST
  6468.      From: Vern Paxson <vern>
  6469.      
  6470.      > I'm working for the Swedish team translating GNU program, and I'm currently
  6471.      > working with flex. I have a few questions about some of the messages which
  6472.      > I hope you can answer.
  6473.      
  6474.      All of the things you're wondering about, by the way, concerning flex
  6475.      internals - probably the only person who understands what they mean in
  6476.      English is me!  So I wouldn't worry too much about getting them right.
  6477.      That said ...
  6478.      
  6479.      > #: main.c:545
  6480.      > msgid "  %d protos created\n"
  6481.      >
  6482.      > Does proto mean prototype?
  6483.      
  6484.      Yes - prototypes of state compression tables.
  6485.      
  6486.      > #: main.c:539
  6487.      > msgid "  %d/%d (peak %d) template nxt-chk entries created\n"
  6488.      >
  6489.      > Here I'm mainly puzzled by 'nxt-chk'. I guess it means 'next-check'. (?)
  6490.      > However, 'template next-check entries' doesn't make much sense to me. To be
  6491.      > able to find a good translation I need to know a little bit more about it.
  6492.      
  6493.      There is a scheme in the Aho/Sethi/Ullman compiler book for compressing
  6494.      scanner tables.  It involves creating two pairs of tables.  The first has
  6495.      "base" and "default" entries, the second has "next" and "check" entries.
  6496.      The "base" entry is indexed by the current state and yields an index into
  6497.      the next/check table.  The "default" entry gives what to do if the state
  6498.      transition isn't found in next/check.  The "next" entry gives the next
  6499.      state to enter, but only if the "check" entry verifies that this entry is
  6500.      correct for the current state.  Flex creates templates of series of
  6501.      next/check entries and then encodes differences from these templates as a
  6502.      way to compress the tables.
  6503.      
  6504.      > #: main.c:533
  6505.      > msgid "  %d/%d base-def entries created\n"
  6506.      >
  6507.      > The same problem here for 'base-def'.
  6508.      
  6509.      See above.
  6510.      
  6511.              Vern
  6512. </pre>
  6513. </pre>
  6514. <!-- TODO: Evaluate this faq. -->
  6515. <div class="node">
  6516. <p><hr>
  6517. <a name="unput()-messes-up-yy_at_bol"></a>
  6518. <a name="unput_0028_0029-messes-up-yy_005fat_005fbol"></a>
  6519. Next: <a rel="next" accesskey="n" href="#The-_007c-operator-is-not-doing-what-I-want">The | operator is not doing what I want</a>,
  6520. Previous: <a rel="previous" accesskey="p" href="#Can-you-discuss-some-flex-internals_003f">Can you discuss some flex internals?</a>,
  6521. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6522.  
  6523. </div>
  6524.  
  6525. <h3 class="unnumberedsec">unput() messes up yy_at_bol</h3>
  6526.  
  6527. <pre class="example"><pre class="verbatim">     
  6528.      To: Xinying Li <xli@npac.syr.edu>
  6529.      Subject: Re: FLEX ?
  6530.      In-reply-to: Your message of Wed, 13 Nov 1996 17:28:38 PST.
  6531.      Date: Wed, 13 Nov 1996 19:51:54 PST
  6532.      From: Vern Paxson <vern>
  6533.      
  6534.      > "unput()" them to input flow, question occurs. If I do this after I scan
  6535.      > a carriage, the variable "YY_CURRENT_BUFFER->yy_at_bol" is changed. That
  6536.      > means the carriage flag has gone.
  6537.      
  6538.      You can control this by calling yy_set_bol().  It's described in the manual.
  6539.      
  6540.      >      And if in pre-reading it goes to the end of file, is anything done
  6541.      > to control the end of curren buffer and end of file?
  6542.      
  6543.      No, there's no way to put back an end-of-file.
  6544.      
  6545.      >      By the way I am using flex 2.5.2 and using the "-l".
  6546.      
  6547.      The latest release is 2.5.4, by the way.  It fixes some bugs in 2.5.2 and
  6548.      2.5.3.  You can get it from ftp.ee.lbl.gov.
  6549.      
  6550.              Vern
  6551. </pre>
  6552. </pre>
  6553. <!-- TODO: Evaluate this faq. -->
  6554. <div class="node">
  6555. <p><hr>
  6556. <a name="The-%7c-operator-is-not-doing-what-I-want"></a>
  6557. <a name="The-_007c-operator-is-not-doing-what-I-want"></a>
  6558. Next: <a rel="next" accesskey="n" href="#Why-can_0027t-flex-understand-this-variable-trailing-context-pattern_003f">Why can't flex understand this variable trailing context pattern?</a>,
  6559. Previous: <a rel="previous" accesskey="p" href="#unput_0028_0029-messes-up-yy_005fat_005fbol">unput() messes up yy_at_bol</a>,
  6560. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6561.  
  6562. </div>
  6563.  
  6564. <h3 class="unnumberedsec">The | operator is not doing what I want</h3>
  6565.  
  6566. <pre class="example"><pre class="verbatim">     
  6567.      To: Alain.ISSARD@st.com
  6568.      Subject: Re: Start condition with FLEX
  6569.      In-reply-to: Your message of Mon, 18 Nov 1996 09:45:02 PST.
  6570.      Date: Mon, 18 Nov 1996 10:41:34 PST
  6571.      From: Vern Paxson <vern>
  6572.      
  6573.      > I am not able to use the start condition scope and to use the | (OR) with
  6574.      > rules having start conditions.
  6575.      
  6576.      The problem is that if you use '|' as a regular expression operator, for
  6577.      example "a|b" meaning "match either 'a' or 'b'", then it must *not* have
  6578.      any blanks around it.  If you instead want the special '|' *action* (which
  6579.      from your scanner appears to be the case), which is a way of giving two
  6580.      different rules the same action:
  6581.      
  6582.          foo    |
  6583.          bar    matched_foo_or_bar();
  6584.      
  6585.      then '|' *must* be separated from the first rule by whitespace and *must*
  6586.      be followed by a new line.  You *cannot* write it as:
  6587.      
  6588.          foo | bar    matched_foo_or_bar();
  6589.      
  6590.      even though you might think you could because yacc supports this syntax.
  6591.      The reason for this unfortunately incompatibility is historical, but it's
  6592.      unlikely to be changed.
  6593.      
  6594.      Your problems with start condition scope are simply due to syntax errors
  6595.      from your use of '|' later confusing flex.
  6596.      
  6597.      Let me know if you still have problems.
  6598.      
  6599.              Vern
  6600. </pre>
  6601. </pre>
  6602. <!-- TODO: Evaluate this faq. -->
  6603. <div class="node">
  6604. <p><hr>
  6605. <a name="Why-can't-flex-understand-this-variable-trailing-context-pattern%3f"></a>
  6606. <a name="Why-can_0027t-flex-understand-this-variable-trailing-context-pattern_003f"></a>
  6607. Next: <a rel="next" accesskey="n" href="#The-_005e-operator-isn_0027t-working">The ^ operator isn't working</a>,
  6608. Previous: <a rel="previous" accesskey="p" href="#The-_007c-operator-is-not-doing-what-I-want">The | operator is not doing what I want</a>,
  6609. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6610.  
  6611. </div>
  6612.  
  6613. <h3 class="unnumberedsec">Why can't flex understand this variable trailing context pattern?</h3>
  6614.  
  6615. <pre class="example"><pre class="verbatim">     
  6616.      To: Gregory Margo <gmargo@newton.vip.best.com>
  6617.      Subject: Re: flex-2.5.3 bug report
  6618.      In-reply-to: Your message of Sat, 23 Nov 1996 16:50:09 PST.
  6619.      Date: Sat, 23 Nov 1996 17:07:32 PST
  6620.      From: Vern Paxson <vern>
  6621.      
  6622.      > Enclosed is a lex file that "real" lex will process, but I cannot get
  6623.      > flex to process it.  Could you try it and maybe point me in the right direction?
  6624.      
  6625.      Your problem is that some of the definitions in the scanner use the '/'
  6626.      trailing context operator, and have it enclosed in ()'s.  Flex does not
  6627.      allow this operator to be enclosed in ()'s because doing so allows undefined
  6628.      regular expressions such as "(a/b)+".  So the solution is to remove the
  6629.      parentheses.  Note that you must also be building the scanner with the -l
  6630.      option for AT&T lex compatibility.  Without this option, flex automatically
  6631.      encloses the definitions in parentheses.
  6632.      
  6633.              Vern
  6634. </pre>
  6635. </pre>
  6636. <!-- TODO: Evaluate this faq. -->
  6637. <div class="node">
  6638. <p><hr>
  6639. <a name="The-%5e-operator-isn't-working"></a>
  6640. <a name="The-_005e-operator-isn_0027t-working"></a>
  6641. Next: <a rel="next" accesskey="n" href="#Trailing-context-is-getting-confused-with-trailing-optional-patterns">Trailing context is getting confused with trailing optional patterns</a>,
  6642. Previous: <a rel="previous" accesskey="p" href="#Why-can_0027t-flex-understand-this-variable-trailing-context-pattern_003f">Why can't flex understand this variable trailing context pattern?</a>,
  6643. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6644.  
  6645. </div>
  6646.  
  6647. <h3 class="unnumberedsec">The ^ operator isn't working</h3>
  6648.  
  6649. <pre class="example"><pre class="verbatim">     
  6650.      To: Thomas Hadig <hadig@toots.physik.rwth-aachen.de>
  6651.      Subject: Re: Flex Bug ?
  6652.      In-reply-to: Your message of Tue, 26 Nov 1996 14:35:01 PST.
  6653.      Date: Tue, 26 Nov 1996 11:15:05 PST
  6654.      From: Vern Paxson <vern>
  6655.      
  6656.      > In my lexer code, i have the line :
  6657.      > ^\*.*          { }
  6658.      >
  6659.      > Thus all lines starting with an astrix (*) are comment lines.
  6660.      > This does not work !
  6661.      
  6662.      I can't get this problem to reproduce - it works fine for me.  Note
  6663.      though that if what you have is slightly different:
  6664.      
  6665.          COMMENT    ^\*.*
  6666.          %%
  6667.          {COMMENT}    { }
  6668.      
  6669.      then it won't work, because flex pushes back macro definitions enclosed
  6670.      in ()'s, so the rule becomes
  6671.      
  6672.          (^\*.*)        { }
  6673.      
  6674.      and now that the '^' operator is not at the immediate beginning of the
  6675.      line, it's interpreted as just a regular character.  You can avoid this
  6676.      behavior by using the "-l" lex-compatibility flag, or "%option lex-compat".
  6677.      
  6678.              Vern
  6679. </pre>
  6680. </pre>
  6681. <!-- TODO: Evaluate this faq. -->
  6682. <div class="node">
  6683. <p><hr>
  6684. <a name="Trailing-context-is-getting-confused-with-trailing-optional-patterns"></a>
  6685. Next: <a rel="next" accesskey="n" href="#Is-flex-GNU-or-not_003f">Is flex GNU or not?</a>,
  6686. Previous: <a rel="previous" accesskey="p" href="#The-_005e-operator-isn_0027t-working">The ^ operator isn't working</a>,
  6687. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6688.  
  6689. </div>
  6690.  
  6691. <h3 class="unnumberedsec">Trailing context is getting confused with trailing optional patterns</h3>
  6692.  
  6693. <pre class="example"><pre class="verbatim">     
  6694.      To: Adoram Rogel <adoram@hybridge.com>
  6695.      Subject: Re: Flex 2.5.4 BOF ???
  6696.      In-reply-to: Your message of Tue, 26 Nov 1996 16:10:41 PST.
  6697.      Date: Wed, 27 Nov 1996 10:56:25 PST
  6698.      From: Vern Paxson <vern>
  6699.      
  6700.      >     Organization(s)?/[a-z]
  6701.      >
  6702.      > This matched "Organizations" (looking in debug mode, the trailing s
  6703.      > was matched with trailing context instead of the optional (s) in the
  6704.      > end of the word.
  6705.      
  6706.      That should only happen with lex.  Flex can properly match this pattern.
  6707.      (That might be what you're saying, I'm just not sure.)
  6708.      
  6709.      > Is there a way to avoid this dangerous trailing context problem ?
  6710.      
  6711.      Unfortunately, there's no easy way.  On the other hand, I don't see why
  6712.      it should be a problem.  Lex's matching is clearly wrong, and I'd hope
  6713.      that usually the intent remains the same as expressed with the pattern,
  6714.      so flex's matching will be correct.
  6715.      
  6716.              Vern
  6717. </pre>
  6718. </pre>
  6719. <!-- TODO: Evaluate this faq. -->
  6720. <div class="node">
  6721. <p><hr>
  6722. <a name="Is-flex-GNU-or-not%3f"></a>
  6723. <a name="Is-flex-GNU-or-not_003f"></a>
  6724. Next: <a rel="next" accesskey="n" href="#ERASEME53">ERASEME53</a>,
  6725. Previous: <a rel="previous" accesskey="p" href="#Trailing-context-is-getting-confused-with-trailing-optional-patterns">Trailing context is getting confused with trailing optional patterns</a>,
  6726. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6727.  
  6728. </div>
  6729.  
  6730. <h3 class="unnumberedsec">Is flex GNU or not?</h3>
  6731.  
  6732. <pre class="example"><pre class="verbatim">     
  6733.      To: Cameron MacKinnon <mackin@interlog.com>
  6734.      Subject: Re: Flex documentation bug
  6735.      In-reply-to: Your message of Mon, 02 Dec 1996 00:07:08 PST.
  6736.      Date: Sun, 01 Dec 1996 22:29:39 PST
  6737.      From: Vern Paxson <vern>
  6738.      
  6739.      > I'm not sure how or where to submit bug reports (documentation or
  6740.      > otherwise) for the GNU project stuff ...
  6741.      
  6742.      Well, strictly speaking flex isn't part of the GNU project.  They just
  6743.      distribute it because no one's written a decent GPL'd lex replacement.
  6744.      So you should send bugs directly to me.  Those sent to the GNU folks
  6745.      sometimes find there way to me, but some may drop between the cracks.
  6746.      
  6747.      > In GNU Info, under the section 'Start Conditions', and also in the man
  6748.      > page (mine's dated April '95) is a nice little snippet showing how to
  6749.      > parse C quoted strings into a buffer, defined to be MAX_STR_CONST in
  6750.      > size. Unfortunately, no overflow checking is ever done ...
  6751.      
  6752.      This is already mentioned in the manual:
  6753.      
  6754.      Finally, here's an example of how to  match  C-style  quoted
  6755.      strings using exclusive start conditions, including expanded
  6756.      escape sequences (but not including checking  for  a  string
  6757.      that's too long):
  6758.      
  6759.      The reason for not doing the overflow checking is that it will needlessly
  6760.      clutter up an example whose main purpose is just to demonstrate how to
  6761.      use flex.
  6762.      
  6763.      The latest release is 2.5.4, by the way, available from ftp.ee.lbl.gov.
  6764.      
  6765.              Vern
  6766. </pre>
  6767. </pre>
  6768. <!-- TODO: Evaluate this faq. -->
  6769. <div class="node">
  6770. <p><hr>
  6771. <a name="ERASEME53"></a>
  6772. Next: <a rel="next" accesskey="n" href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>,
  6773. Previous: <a rel="previous" accesskey="p" href="#Is-flex-GNU-or-not_003f">Is flex GNU or not?</a>,
  6774. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6775.  
  6776. </div>
  6777.  
  6778. <h3 class="unnumberedsec">ERASEME53</h3>
  6779.  
  6780. <pre class="example"><pre class="verbatim">     
  6781.      To: tsv@cs.UManitoba.CA
  6782.      Subject: Re: Flex (reg)..
  6783.      In-reply-to: Your message of Thu, 06 Mar 1997 23:50:16 PST.
  6784.      Date: Thu, 06 Mar 1997 15:54:19 PST
  6785.      From: Vern Paxson <vern>
  6786.      
  6787.      > [:alpha:] ([:alnum:] | \\_)*
  6788.      
  6789.      If your rule really has embedded blanks as shown above, then it won't
  6790.      work, as the first blank delimits the rule from the action.  (It wouldn't
  6791.      even compile ...)  You need instead:
  6792.      
  6793.      [:alpha:]([:alnum:]|\\_)*
  6794.      
  6795.      and that should work fine - there's no restriction on what can go inside
  6796.      of ()'s except for the trailing context operator, '/'.
  6797.      
  6798.              Vern
  6799. </pre>
  6800. </pre>
  6801. <!-- TODO: Evaluate this faq. -->
  6802. <div class="node">
  6803. <p><hr>
  6804. <a name="I-need-to-scan-if-then-else-blocks-and-while-loops"></a>
  6805. <a name="I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops"></a>
  6806. Next: <a rel="next" accesskey="n" href="#ERASEME55">ERASEME55</a>,
  6807. Previous: <a rel="previous" accesskey="p" href="#ERASEME53">ERASEME53</a>,
  6808. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6809.  
  6810. </div>
  6811.  
  6812. <h3 class="unnumberedsec">I need to scan if-then-else blocks and while loops</h3>
  6813.  
  6814. <pre class="example"><pre class="verbatim">     
  6815.      To: "Mike Stolnicki" <mstolnic@ford.com>
  6816.      Subject: Re: FLEX help
  6817.      In-reply-to: Your message of Fri, 30 May 1997 13:33:27 PDT.
  6818.      Date: Fri, 30 May 1997 10:46:35 PDT
  6819.      From: Vern Paxson <vern>
  6820.      
  6821.      > We'd like to add "if-then-else", "while", and "for" statements to our
  6822.      > language ...
  6823.      > We've investigated many possible solutions.  The one solution that seems
  6824.      > the most reasonable involves knowing the position of a TOKEN in yyin.
  6825.      
  6826.      I strongly advise you to instead build a parse tree (abstract syntax tree)
  6827.      and loop over that instead.  You'll find this has major benefits in keeping
  6828.      your interpreter simple and extensible.
  6829.      
  6830.      That said, the functionality you mention for get_position and set_position
  6831.      have been on the to-do list for a while.  As flex is a purely spare-time
  6832.      project for me, no guarantees when this will be added (in particular, it
  6833.      for sure won't be for many months to come).
  6834.      
  6835.              Vern
  6836. </pre>
  6837. </pre>
  6838. <!-- TODO: Evaluate this faq. -->
  6839. <div class="node">
  6840. <p><hr>
  6841. <a name="ERASEME55"></a>
  6842. Next: <a rel="next" accesskey="n" href="#ERASEME56">ERASEME56</a>,
  6843. Previous: <a rel="previous" accesskey="p" href="#I-need-to-scan-if_002dthen_002delse-blocks-and-while-loops">I need to scan if-then-else blocks and while loops</a>,
  6844. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6845.  
  6846. </div>
  6847.  
  6848. <h3 class="unnumberedsec">ERASEME55</h3>
  6849.  
  6850. <pre class="example"><pre class="verbatim">     
  6851.      To: Colin Paul Adams <colin@colina.demon.co.uk>
  6852.      Subject: Re: Flex C++ classes and Bison
  6853.      In-reply-to: Your message of 09 Aug 1997 17:11:41 PDT.
  6854.      Date: Fri, 15 Aug 1997 10:48:19 PDT
  6855.      From: Vern Paxson <vern>
  6856.      
  6857.      > #define YY_DECL   int yylex (YYSTYPE *lvalp, struct parser_control
  6858.      > *parm)
  6859.      >
  6860.      > I have been trying  to get this to work as a C++ scanner, but it does
  6861.      > not appear to be possible (warning that it matches no declarations in
  6862.      > yyFlexLexer, or something like that).
  6863.      >
  6864.      > Is this supposed to be possible, or is it being worked on (I DID
  6865.      > notice the comment that scanner classes are still experimental, so I'm
  6866.      > not too hopeful)?
  6867.      
  6868.      What you need to do is derive a subclass from yyFlexLexer that provides
  6869.      the above yylex() method, squirrels away lvalp and parm into member
  6870.      variables, and then invokes yyFlexLexer::yylex() to do the regular scanning.
  6871.      
  6872.              Vern
  6873. </pre>
  6874. </pre>
  6875. <!-- TODO: Evaluate this faq. -->
  6876. <div class="node">
  6877. <p><hr>
  6878. <a name="ERASEME56"></a>
  6879. Next: <a rel="next" accesskey="n" href="#ERASEME57">ERASEME57</a>,
  6880. Previous: <a rel="previous" accesskey="p" href="#ERASEME55">ERASEME55</a>,
  6881. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6882.  
  6883. </div>
  6884.  
  6885. <h3 class="unnumberedsec">ERASEME56</h3>
  6886.  
  6887. <pre class="example"><pre class="verbatim">     
  6888.      To: Mikael.Latvala@lmf.ericsson.se
  6889.      Subject: Re: Possible mistake in Flex v2.5 document
  6890.      In-reply-to: Your message of Fri, 05 Sep 1997 16:07:24 PDT.
  6891.      Date: Fri, 05 Sep 1997 10:01:54 PDT
  6892.      From: Vern Paxson <vern>
  6893.      
  6894.      > In that example you show how to count comment lines when using
  6895.      > C style /* ... */ comments. My question is, shouldn't you take into
  6896.      > account a scenario where end of a comment marker occurs inside
  6897.      > character or string literals?
  6898.      
  6899.      The scanner certainly needs to also scan character and string literals.
  6900.      However it does that (there's an example in the man page for strings), the
  6901.      lexer will recognize the beginning of the literal before it runs across the
  6902.      embedded "/*".  Consequently, it will finish scanning the literal before it
  6903.      even considers the possibility of matching "/*".
  6904.      
  6905.      Example:
  6906.      
  6907.          '([^']*|{ESCAPE_SEQUENCE})'
  6908.      
  6909.      will match all the text between the ''s (inclusive).  So the lexer
  6910.      considers this as a token beginning at the first ', and doesn't even
  6911.      attempt to match other tokens inside it.
  6912.      
  6913.      I thinnk this subtlety is not worth putting in the manual, as I suspect
  6914.      it would confuse more people than it would enlighten.
  6915.      
  6916.              Vern
  6917. </pre>
  6918. </pre>
  6919. <!-- TODO: Evaluate this faq. -->
  6920. <div class="node">
  6921. <p><hr>
  6922. <a name="ERASEME57"></a>
  6923. Next: <a rel="next" accesskey="n" href="#Is-there-a-repository-for-flex-scanners_003f">Is there a repository for flex scanners?</a>,
  6924. Previous: <a rel="previous" accesskey="p" href="#ERASEME56">ERASEME56</a>,
  6925. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6926.  
  6927. </div>
  6928.  
  6929. <h3 class="unnumberedsec">ERASEME57</h3>
  6930.  
  6931. <pre class="example"><pre class="verbatim">     
  6932.      To: "Marty Leisner" <leisner@sdsp.mc.xerox.com>
  6933.      Subject: Re: flex limitations
  6934.      In-reply-to: Your message of Sat, 06 Sep 1997 11:27:21 PDT.
  6935.      Date: Mon, 08 Sep 1997 11:38:08 PDT
  6936.      From: Vern Paxson <vern>
  6937.      
  6938.      > %%
  6939.      > [a-zA-Z]+       /* skip a line */
  6940.      >                 {  printf("got %s\n", yytext); }
  6941.      > %%
  6942.      
  6943.      What version of flex are you using?  If I feed this to 2.5.4, it complains:
  6944.      
  6945.          "bug.l", line 5: EOF encountered inside an action
  6946.          "bug.l", line 5: unrecognized rule
  6947.          "bug.l", line 5: fatal parse error
  6948.      
  6949.      Not the world's greatest error message, but it manages to flag the problem.
  6950.      
  6951.      (With the introduction of start condition scopes, flex can't accommodate
  6952.      an action on a separate line, since it's ambiguous with an indented rule.)
  6953.      
  6954.      You can get 2.5.4 from ftp.ee.lbl.gov.
  6955.      
  6956.              Vern
  6957. </pre>
  6958. </pre>
  6959. <!-- TODO: Evaluate this faq. -->
  6960. <div class="node">
  6961. <p><hr>
  6962. <a name="Is-there-a-repository-for-flex-scanners%3f"></a>
  6963. <a name="Is-there-a-repository-for-flex-scanners_003f"></a>
  6964. Next: <a rel="next" accesskey="n" href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>,
  6965. Previous: <a rel="previous" accesskey="p" href="#ERASEME57">ERASEME57</a>,
  6966. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6967.  
  6968. </div>
  6969.  
  6970. <h3 class="unnumberedsec">Is there a repository for flex scanners?</h3>
  6971.  
  6972. <p>Not that we know of. You might try asking on comp.compilers.
  6973.  
  6974. <!-- TODO: Evaluate this faq. -->
  6975. <div class="node">
  6976. <p><hr>
  6977. <a name="How-can-I-conditionally-compile-or-preprocess-my-flex-input-file%3f"></a>
  6978. <a name="How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f"></a>
  6979. Next: <a rel="next" accesskey="n" href="#Where-can-I-find-grammars-for-lex-and-yacc_003f">Where can I find grammars for lex and yacc?</a>,
  6980. Previous: <a rel="previous" accesskey="p" href="#Is-there-a-repository-for-flex-scanners_003f">Is there a repository for flex scanners?</a>,
  6981. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6982.  
  6983. </div>
  6984.  
  6985. <h3 class="unnumberedsec">How can I conditionally compile or preprocess my flex input file?</h3>
  6986.  
  6987. <p>Flex doesn't have a preprocessor like C does.  You might try using m4, or the C
  6988. preprocessor plus a sed script to clean up the result.
  6989.  
  6990. <!-- TODO: Evaluate this faq. -->
  6991. <div class="node">
  6992. <p><hr>
  6993. <a name="Where-can-I-find-grammars-for-lex-and-yacc%3f"></a>
  6994. <a name="Where-can-I-find-grammars-for-lex-and-yacc_003f"></a>
  6995. Next: <a rel="next" accesskey="n" href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>,
  6996. Previous: <a rel="previous" accesskey="p" href="#How-can-I-conditionally-compile-or-preprocess-my-flex-input-file_003f">How can I conditionally compile or preprocess my flex input file?</a>,
  6997. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  6998.  
  6999. </div>
  7000.  
  7001. <h3 class="unnumberedsec">Where can I find grammars for lex and yacc?</h3>
  7002.  
  7003. <p>In the sources for flex and bison.
  7004.  
  7005. <!-- TODO: Evaluate this faq. -->
  7006. <div class="node">
  7007. <p><hr>
  7008. <a name="I-get-an-end-of-buffer-message-for-each-character-scanned."></a>
  7009. <a name="I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e"></a>
  7010. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d62">unnamed-faq-62</a>,
  7011. Previous: <a rel="previous" accesskey="p" href="#Where-can-I-find-grammars-for-lex-and-yacc_003f">Where can I find grammars for lex and yacc?</a>,
  7012. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7013.  
  7014. </div>
  7015.  
  7016. <h3 class="unnumberedsec">I get an end-of-buffer message for each character scanned.</h3>
  7017.  
  7018. <p>This will happen if your LexerInput() function returns only one character
  7019. at a time, which can happen either if you're scanner is "interactive", or
  7020. if the streams library on your platform always returns 1 for yyin->gcount().
  7021.  
  7022.    <p>Solution: override LexerInput() with a version that returns whole buffers.
  7023.  
  7024. <!-- TODO: Evaluate this faq. -->
  7025. <div class="node">
  7026. <p><hr>
  7027. <a name="unnamed-faq-62"></a>
  7028. <a name="unnamed_002dfaq_002d62"></a>
  7029. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d63">unnamed-faq-63</a>,
  7030. Previous: <a rel="previous" accesskey="p" href="#I-get-an-end_002dof_002dbuffer-message-for-each-character-scanned_002e">I get an end-of-buffer message for each character scanned.</a>,
  7031. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7032.  
  7033. </div>
  7034.  
  7035. <h3 class="unnumberedsec">unnamed-faq-62</h3>
  7036.  
  7037. <pre class="example"><pre class="verbatim">     
  7038.      To: Georg.Rehm@CL-KI.Uni-Osnabrueck.DE
  7039.      Subject: Re: Flex maximums
  7040.      In-reply-to: Your message of Mon, 17 Nov 1997 17:16:06 PST.
  7041.      Date: Mon, 17 Nov 1997 17:16:15 PST
  7042.      From: Vern Paxson <vern>
  7043.      
  7044.      > I took a quick look into the flex-sources and altered some #defines in
  7045.      > flexdefs.h:
  7046.      >
  7047.      >     #define INITIAL_MNS 64000
  7048.      >     #define MNS_INCREMENT 1024000
  7049.      >     #define MAXIMUM_MNS 64000
  7050.      
  7051.      The things to fix are to add a couple of zeroes to:
  7052.      
  7053.      #define JAMSTATE -32766 /* marks a reference to the state that always jams */
  7054.      #define MAXIMUM_MNS 31999
  7055.      #define BAD_SUBSCRIPT -32767
  7056.      #define MAX_SHORT 32700
  7057.      
  7058.      and, if you get complaints about too many rules, make the following change too:
  7059.      
  7060.          #define YY_TRAILING_MASK 0x200000
  7061.          #define YY_TRAILING_HEAD_MASK 0x400000
  7062.      
  7063.      - Vern
  7064. </pre>
  7065. </pre>
  7066. <!-- TODO: Evaluate this faq. -->
  7067. <div class="node">
  7068. <p><hr>
  7069. <a name="unnamed-faq-63"></a>
  7070. <a name="unnamed_002dfaq_002d63"></a>
  7071. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d64">unnamed-faq-64</a>,
  7072. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d62">unnamed-faq-62</a>,
  7073. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7074.  
  7075. </div>
  7076.  
  7077. <h3 class="unnumberedsec">unnamed-faq-63</h3>
  7078.  
  7079. <pre class="example"><pre class="verbatim">     
  7080.      To: jimmey@lexis-nexis.com (Jimmey Todd)
  7081.      Subject: Re: FLEX question regarding istream vs ifstream
  7082.      In-reply-to: Your message of Mon, 08 Dec 1997 15:54:15 PST.
  7083.      Date: Mon, 15 Dec 1997 13:21:35 PST
  7084.      From: Vern Paxson <vern>
  7085.      
  7086.      >         stdin_handle = YY_CURRENT_BUFFER;
  7087.      >         ifstream fin( "aFile" );
  7088.      >         yy_switch_to_buffer( yy_create_buffer( fin, YY_BUF_SIZE ) );
  7089.      >
  7090.      > What I'm wanting to do, is pass the contents of a file thru one set
  7091.      > of rules and then pass stdin thru another set... It works great if, I
  7092.      > don't use the C++ classes. But since everything else that I'm doing is
  7093.      > in C++, I thought I'd be consistent.
  7094.      >
  7095.      > The problem is that 'yy_create_buffer' is expecting an istream* as it's
  7096.      > first argument (as stated in the man page). However, fin is a ifstream
  7097.      > object. Any ideas on what I might be doing wrong? Any help would be
  7098.      > appreciated. Thanks!!
  7099.      
  7100.      You need to pass &fin, to turn it into an ifstream* instead of an ifstream.
  7101.      Then its type will be compatible with the expected istream*, because ifstream
  7102.      is derived from istream.
  7103.      
  7104.              Vern
  7105. </pre>
  7106. </pre>
  7107. <!-- TODO: Evaluate this faq. -->
  7108. <div class="node">
  7109. <p><hr>
  7110. <a name="unnamed-faq-64"></a>
  7111. <a name="unnamed_002dfaq_002d64"></a>
  7112. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d65">unnamed-faq-65</a>,
  7113. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d63">unnamed-faq-63</a>,
  7114. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7115.  
  7116. </div>
  7117.  
  7118. <h3 class="unnumberedsec">unnamed-faq-64</h3>
  7119.  
  7120. <pre class="example"><pre class="verbatim">     
  7121.      To: Enda Fadian <fadiane@piercom.ie>
  7122.      Subject: Re: Question related to Flex man page?
  7123.      In-reply-to: Your message of Tue, 16 Dec 1997 15:17:34 PST.
  7124.      Date: Tue, 16 Dec 1997 14:17:09 PST
  7125.      From: Vern Paxson <vern>
  7126.      
  7127.      > Can you explain to me what is ment by a long-jump in relation to flex?
  7128.      
  7129.      Using the longjmp() function while inside yylex() or a routine called by it.
  7130.      
  7131.      > what is the flex activation frame.
  7132.      
  7133.      Just yylex()'s stack frame.
  7134.      
  7135.      > As far as I can see yyrestart will bring me back to the sart of the input
  7136.      > file and using flex++ isnot really an option!
  7137.      
  7138.      No, yyrestart() doesn't imply a rewind, even though its name might sound
  7139.      like it does.  It tells the scanner to flush its internal buffers and
  7140.      start reading from the given file at its present location.
  7141.      
  7142.              Vern
  7143. </pre>
  7144. </pre>
  7145. <!-- TODO: Evaluate this faq. -->
  7146. <div class="node">
  7147. <p><hr>
  7148. <a name="unnamed-faq-65"></a>
  7149. <a name="unnamed_002dfaq_002d65"></a>
  7150. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d66">unnamed-faq-66</a>,
  7151. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d64">unnamed-faq-64</a>,
  7152. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7153.  
  7154. </div>
  7155.  
  7156. <h3 class="unnumberedsec">unnamed-faq-65</h3>
  7157.  
  7158. <pre class="example"><pre class="verbatim">     
  7159.      To: hassan@larc.info.uqam.ca (Hassan Alaoui)
  7160.      Subject: Re: Need urgent Help
  7161.      In-reply-to: Your message of Sat, 20 Dec 1997 19:38:19 PST.
  7162.      Date: Sun, 21 Dec 1997 21:30:46 PST
  7163.      From: Vern Paxson <vern>
  7164.      
  7165.      > /usr/lib/yaccpar: In function `int yyparse()':
  7166.      > /usr/lib/yaccpar:184: warning: implicit declaration of function `int yylex(...)'
  7167.      >
  7168.      > ld: Undefined symbol
  7169.      >    _yylex
  7170.      >    _yyparse
  7171.      >    _yyin
  7172.      
  7173.      This is a known problem with Solaris C++ (and/or Solaris yacc).  I believe
  7174.      the fix is to explicitly insert some 'extern "C"' statements for the
  7175.      corresponding routines/symbols.
  7176.      
  7177.              Vern
  7178. </pre>
  7179. </pre>
  7180. <!-- TODO: Evaluate this faq. -->
  7181. <div class="node">
  7182. <p><hr>
  7183. <a name="unnamed-faq-66"></a>
  7184. <a name="unnamed_002dfaq_002d66"></a>
  7185. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d67">unnamed-faq-67</a>,
  7186. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d65">unnamed-faq-65</a>,
  7187. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7188.  
  7189. </div>
  7190.  
  7191. <h3 class="unnumberedsec">unnamed-faq-66</h3>
  7192.  
  7193. <pre class="example"><pre class="verbatim">     
  7194.      To: mc0307@mclink.it
  7195.      Cc: gnu@prep.ai.mit.edu
  7196.      Subject: Re: [mc0307@mclink.it: Help request]
  7197.      In-reply-to: Your message of Fri, 12 Dec 1997 17:57:29 PST.
  7198.      Date: Sun, 21 Dec 1997 22:33:37 PST
  7199.      From: Vern Paxson <vern>
  7200.      
  7201.      > This is my definition for float and integer types:
  7202.      > . . .
  7203.      > NZD          [1-9]
  7204.      > ...
  7205.      > I've tested my program on other lex version (on UNIX Sun Solaris an HP
  7206.      > UNIX) and it work well, so I think that my definitions are correct.
  7207.      > There are any differences between Lex and Flex?
  7208.      
  7209.      There are indeed differences, as discussed in the man page.  The one
  7210.      you are probably running into is that when flex expands a name definition,
  7211.      it puts parentheses around the expansion, while lex does not.  There's
  7212.      an example in the man page of how this can lead to different matching.
  7213.      Flex's behavior complies with the POSIX standard (or at least with the
  7214.      last POSIX draft I saw).
  7215.      
  7216.              Vern
  7217. </pre>
  7218. </pre>
  7219. <!-- TODO: Evaluate this faq. -->
  7220. <div class="node">
  7221. <p><hr>
  7222. <a name="unnamed-faq-67"></a>
  7223. <a name="unnamed_002dfaq_002d67"></a>
  7224. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d68">unnamed-faq-68</a>,
  7225. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d66">unnamed-faq-66</a>,
  7226. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7227.  
  7228. </div>
  7229.  
  7230. <h3 class="unnumberedsec">unnamed-faq-67</h3>
  7231.  
  7232. <pre class="example"><pre class="verbatim">     
  7233.      To: hassan@larc.info.uqam.ca (Hassan Alaoui)
  7234.      Subject: Re: Thanks
  7235.      In-reply-to: Your message of Mon, 22 Dec 1997 16:06:35 PST.
  7236.      Date: Mon, 22 Dec 1997 14:35:05 PST
  7237.      From: Vern Paxson <vern>
  7238.      
  7239.      > Thank you very much for your help. I compile and link well with C++ while
  7240.      > declaring 'yylex ...' extern, But a little problem remains. I get a
  7241.      > segmentation default when executing ( I linked with lfl library) while it
  7242.      > works well when using LEX instead of flex. Do you have some ideas about the
  7243.      > reason for this ?
  7244.      
  7245.      The one possible reason for this that comes to mind is if you've defined
  7246.      yytext as "extern char yytext[]" (which is what lex uses) instead of
  7247.      "extern char *yytext" (which is what flex uses).  If it's not that, then
  7248.      I'm afraid I don't know what the problem might be.
  7249.      
  7250.              Vern
  7251. </pre>
  7252. </pre>
  7253. <!-- TODO: Evaluate this faq. -->
  7254. <div class="node">
  7255. <p><hr>
  7256. <a name="unnamed-faq-68"></a>
  7257. <a name="unnamed_002dfaq_002d68"></a>
  7258. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d69">unnamed-faq-69</a>,
  7259. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d67">unnamed-faq-67</a>,
  7260. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7261.  
  7262. </div>
  7263.  
  7264. <h3 class="unnumberedsec">unnamed-faq-68</h3>
  7265.  
  7266. <pre class="example"><pre class="verbatim">     
  7267.      To: "Bart Niswonger" <NISWONGR@almaden.ibm.com>
  7268.      Subject: Re: flex 2.5: c++ scanners & start conditions
  7269.      In-reply-to: Your message of Tue, 06 Jan 1998 10:34:21 PST.
  7270.      Date: Tue, 06 Jan 1998 19:19:30 PST
  7271.      From: Vern Paxson <vern>
  7272.      
  7273.      > The problem is that when I do this (using %option c++) start
  7274.      > conditions seem to not apply.
  7275.      
  7276.      The BEGIN macro modifies the yy_start variable.  For C scanners, this
  7277.      is a static with scope visible through the whole file.  For C++ scanners,
  7278.      it's a member variable, so it only has visible scope within a member
  7279.      function.  Your lexbegin() routine is not a member function when you
  7280.      build a C++ scanner, so it's not modifying the correct yy_start.  The
  7281.      diagnostic that indicates this is that you found you needed to add
  7282.      a declaration of yy_start in order to get your scanner to compile when
  7283.      using C++; instead, the correct fix is to make lexbegin() a member
  7284.      function (by deriving from yyFlexLexer).
  7285.      
  7286.              Vern
  7287. </pre>
  7288. </pre>
  7289. <!-- TODO: Evaluate this faq. -->
  7290. <div class="node">
  7291. <p><hr>
  7292. <a name="unnamed-faq-69"></a>
  7293. <a name="unnamed_002dfaq_002d69"></a>
  7294. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d70">unnamed-faq-70</a>,
  7295. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d68">unnamed-faq-68</a>,
  7296. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7297.  
  7298. </div>
  7299.  
  7300. <h3 class="unnumberedsec">unnamed-faq-69</h3>
  7301.  
  7302. <pre class="example"><pre class="verbatim">     
  7303.      To: "Boris Zinin" <boris@ippe.rssi.ru>
  7304.      Subject: Re: current position in flex buffer
  7305.      In-reply-to: Your message of Mon, 12 Jan 1998 18:58:23 PST.
  7306.      Date: Mon, 12 Jan 1998 12:03:15 PST
  7307.      From: Vern Paxson <vern>
  7308.      
  7309.      > The problem is how to determine the current position in flex active
  7310.      > buffer when a rule is matched....
  7311.      
  7312.      You will need to keep track of this explicitly, such as by redefining
  7313.      YY_USER_ACTION to count the number of characters matched.
  7314.      
  7315.      The latest flex release, by the way, is 2.5.4, available from ftp.ee.lbl.gov.
  7316.      
  7317.              Vern
  7318. </pre>
  7319. </pre>
  7320. <!-- TODO: Evaluate this faq. -->
  7321. <div class="node">
  7322. <p><hr>
  7323. <a name="unnamed-faq-70"></a>
  7324. <a name="unnamed_002dfaq_002d70"></a>
  7325. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d71">unnamed-faq-71</a>,
  7326. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d69">unnamed-faq-69</a>,
  7327. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7328.  
  7329. </div>
  7330.  
  7331. <h3 class="unnumberedsec">unnamed-faq-70</h3>
  7332.  
  7333. <pre class="example"><pre class="verbatim">     
  7334.      To: Bik.Dhaliwal@bis.org
  7335.      Subject: Re: Flex question
  7336.      In-reply-to: Your message of Mon, 26 Jan 1998 13:05:35 PST.
  7337.      Date: Tue, 27 Jan 1998 22:41:52 PST
  7338.      From: Vern Paxson <vern>
  7339.      
  7340.      > That requirement involves knowing
  7341.      > the character position at which a particular token was matched
  7342.      > in the lexer.
  7343.      
  7344.      The way you have to do this is by explicitly keeping track of where
  7345.      you are in the file, by counting the number of characters scanned
  7346.      for each token (available in yyleng).  It may prove convenient to
  7347.      do this by redefining YY_USER_ACTION, as described in the manual.
  7348.      
  7349.              Vern
  7350. </pre>
  7351. </pre>
  7352. <!-- TODO: Evaluate this faq. -->
  7353. <div class="node">
  7354. <p><hr>
  7355. <a name="unnamed-faq-71"></a>
  7356. <a name="unnamed_002dfaq_002d71"></a>
  7357. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d72">unnamed-faq-72</a>,
  7358. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d70">unnamed-faq-70</a>,
  7359. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7360.  
  7361. </div>
  7362.  
  7363. <h3 class="unnumberedsec">unnamed-faq-71</h3>
  7364.  
  7365. <pre class="example"><pre class="verbatim">     
  7366.      To: Vladimir Alexiev <vladimir@cs.ualberta.ca>
  7367.      Subject: Re: flex: how to control start condition from parser?
  7368.      In-reply-to: Your message of Mon, 26 Jan 1998 05:50:16 PST.
  7369.      Date: Tue, 27 Jan 1998 22:45:37 PST
  7370.      From: Vern Paxson <vern>
  7371.      
  7372.      > It seems useful for the parser to be able to tell the lexer about such
  7373.      > context dependencies, because then they don't have to be limited to
  7374.      > local or sequential context.
  7375.      
  7376.      One way to do this is to have the parser call a stub routine that's
  7377.      included in the scanner's .l file, and consequently that has access ot
  7378.      BEGIN.  The only ugliness is that the parser can't pass in the state
  7379.      it wants, because those aren't visible - but if you don't have many
  7380.      such states, then using a different set of names doesn't seem like
  7381.      to much of a burden.
  7382.      
  7383.      While generating a .h file like you suggests is certainly cleaner,
  7384.      flex development has come to a virtual stand-still :-(, so a workaround
  7385.      like the above is much more pragmatic than waiting for a new feature.
  7386.      
  7387.              Vern
  7388. </pre>
  7389. </pre>
  7390. <!-- TODO: Evaluate this faq. -->
  7391. <div class="node">
  7392. <p><hr>
  7393. <a name="unnamed-faq-72"></a>
  7394. <a name="unnamed_002dfaq_002d72"></a>
  7395. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d73">unnamed-faq-73</a>,
  7396. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d71">unnamed-faq-71</a>,
  7397. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7398.  
  7399. </div>
  7400.  
  7401. <h3 class="unnumberedsec">unnamed-faq-72</h3>
  7402.  
  7403. <pre class="example"><pre class="verbatim">     
  7404.      To: Barbara Denny <denny@3com.com>
  7405.      Subject: Re: freebsd flex bug?
  7406.      In-reply-to: Your message of Fri, 30 Jan 1998 12:00:43 PST.
  7407.      Date: Fri, 30 Jan 1998 12:42:32 PST
  7408.      From: Vern Paxson <vern>
  7409.      
  7410.      > lex.yy.c:1996: parse error before `='
  7411.      
  7412.      This is the key, identifying this error.  (It may help to pinpoint
  7413.      it by using flex -L, so it doesn't generate #line directives in its
  7414.      output.)  I will bet you heavy money that you have a start condition
  7415.      name that is also a variable name, or something like that; flex spits
  7416.      out #define's for each start condition name, mapping them to a number,
  7417.      so you can wind up with:
  7418.      
  7419.          %x foo
  7420.          %%
  7421.              ...
  7422.          %%
  7423.          void bar()
  7424.              {
  7425.              int foo = 3;
  7426.              }
  7427.      
  7428.      and the penultimate will turn into "int 1 = 3" after C preprocessing,
  7429.      since flex will put "#define foo 1" in the generated scanner.
  7430.      
  7431.              Vern
  7432. </pre>
  7433. </pre>
  7434. <!-- TODO: Evaluate this faq. -->
  7435. <div class="node">
  7436. <p><hr>
  7437. <a name="unnamed-faq-73"></a>
  7438. <a name="unnamed_002dfaq_002d73"></a>
  7439. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d74">unnamed-faq-74</a>,
  7440. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d72">unnamed-faq-72</a>,
  7441. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7442.  
  7443. </div>
  7444.  
  7445. <h3 class="unnumberedsec">unnamed-faq-73</h3>
  7446.  
  7447. <pre class="example"><pre class="verbatim">     
  7448.      To: Maurice Petrie <mpetrie@infoscigroup.com>
  7449.      Subject: Re: Lost flex .l file
  7450.      In-reply-to: Your message of Mon, 02 Feb 1998 14:10:01 PST.
  7451.      Date: Mon, 02 Feb 1998 11:15:12 PST
  7452.      From: Vern Paxson <vern>
  7453.      
  7454.      > I am curious as to
  7455.      > whether there is a simple way to backtrack from the generated source to
  7456.      > reproduce the lost list of tokens we are searching on.
  7457.      
  7458.      In theory, it's straight-forward to go from the DFA representation
  7459.      back to a regular-expression representation - the two are isomorphic.
  7460.      In practice, a huge headache, because you have to unpack all the tables
  7461.      back into a single DFA representation, and then write a program to munch
  7462.      on that and translate it into an RE.
  7463.      
  7464.      Sorry for the less-than-happy news ...
  7465.      
  7466.              Vern
  7467. </pre>
  7468. </pre>
  7469. <!-- TODO: Evaluate this faq. -->
  7470. <div class="node">
  7471. <p><hr>
  7472. <a name="unnamed-faq-74"></a>
  7473. <a name="unnamed_002dfaq_002d74"></a>
  7474. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d75">unnamed-faq-75</a>,
  7475. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d73">unnamed-faq-73</a>,
  7476. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7477.  
  7478. </div>
  7479.  
  7480. <h3 class="unnumberedsec">unnamed-faq-74</h3>
  7481.  
  7482. <pre class="example"><pre class="verbatim">     
  7483.      To: jimmey@lexis-nexis.com (Jimmey Todd)
  7484.      Subject: Re: Flex performance question
  7485.      In-reply-to: Your message of Thu, 19 Feb 1998 11:01:17 PST.
  7486.      Date: Thu, 19 Feb 1998 08:48:51 PST
  7487.      From: Vern Paxson <vern>
  7488.      
  7489.      > What I have found, is that the smaller the data chunk, the faster the
  7490.      > program executes. This is the opposite of what I expected. Should this be
  7491.      > happening this way?
  7492.      
  7493.      This is exactly what will happen if your input file has embedded NULs.
  7494.      From the man page:
  7495.      
  7496.      A final note: flex is slow when matching NUL's, particularly
  7497.      when  a  token  contains multiple NUL's.  It's best to write
  7498.      rules which match short amounts of text if it's  anticipated
  7499.      that the text will often include NUL's.
  7500.      
  7501.      So that's the first thing to look for.
  7502.      
  7503.              Vern
  7504. </pre>
  7505. </pre>
  7506. <!-- TODO: Evaluate this faq. -->
  7507. <div class="node">
  7508. <p><hr>
  7509. <a name="unnamed-faq-75"></a>
  7510. <a name="unnamed_002dfaq_002d75"></a>
  7511. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d76">unnamed-faq-76</a>,
  7512. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d74">unnamed-faq-74</a>,
  7513. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7514.  
  7515. </div>
  7516.  
  7517. <h3 class="unnumberedsec">unnamed-faq-75</h3>
  7518.  
  7519. <pre class="example"><pre class="verbatim">     
  7520.      To: jimmey@lexis-nexis.com (Jimmey Todd)
  7521.      Subject: Re: Flex performance question
  7522.      In-reply-to: Your message of Thu, 19 Feb 1998 11:01:17 PST.
  7523.      Date: Thu, 19 Feb 1998 15:42:25 PST
  7524.      From: Vern Paxson <vern>
  7525.      
  7526.      So there are several problems.
  7527.      
  7528.      First, to go fast, you want to match as much text as possible, which
  7529.      your scanners don't in the case that what they're scanning is *not*
  7530.      a <RN> tag.  So you want a rule like:
  7531.      
  7532.          [^<]+
  7533.      
  7534.      Second, C++ scanners are particularly slow if they're interactive,
  7535.      which they are by default.  Using -B speeds it up by a factor of 3-4
  7536.      on my workstation.
  7537.      
  7538.      Third, C++ scanners that use the istream interface are slow, because
  7539.      of how poorly implemented istream's are.  I built two versions of
  7540.      the following scanner:
  7541.      
  7542.          %%
  7543.          .*\n
  7544.          .*
  7545.          %%
  7546.      
  7547.      and the C version inhales a 2.5MB file on my workstation in 0.8 seconds.
  7548.      The C++ istream version, using -B, takes 3.8 seconds.
  7549.      
  7550.              Vern
  7551. </pre>
  7552. </pre>
  7553. <!-- TODO: Evaluate this faq. -->
  7554. <div class="node">
  7555. <p><hr>
  7556. <a name="unnamed-faq-76"></a>
  7557. <a name="unnamed_002dfaq_002d76"></a>
  7558. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d77">unnamed-faq-77</a>,
  7559. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d75">unnamed-faq-75</a>,
  7560. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7561.  
  7562. </div>
  7563.  
  7564. <h3 class="unnumberedsec">unnamed-faq-76</h3>
  7565.  
  7566. <pre class="example"><pre class="verbatim">     
  7567.      To: "Frescatore, David (CRD, TAD)" <frescatore@exc01crdge.crd.ge.com>
  7568.      Subject: Re: FLEX 2.5 & THE YEAR 2000
  7569.      In-reply-to: Your message of Wed, 03 Jun 1998 11:26:22 PDT.
  7570.      Date: Wed, 03 Jun 1998 10:22:26 PDT
  7571.      From: Vern Paxson <vern>
  7572.      
  7573.      > I am researching the Y2K problem with General Electric R&D
  7574.      > and need to know if there are any known issues concerning
  7575.      > the above mentioned software and Y2K regardless of version.
  7576.      
  7577.      There shouldn't be, all it ever does with the date is ask the system
  7578.      for it and then print it out.
  7579.      
  7580.              Vern
  7581. </pre>
  7582. </pre>
  7583. <!-- TODO: Evaluate this faq. -->
  7584. <div class="node">
  7585. <p><hr>
  7586. <a name="unnamed-faq-77"></a>
  7587. <a name="unnamed_002dfaq_002d77"></a>
  7588. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d78">unnamed-faq-78</a>,
  7589. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d76">unnamed-faq-76</a>,
  7590. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7591.  
  7592. </div>
  7593.  
  7594. <h3 class="unnumberedsec">unnamed-faq-77</h3>
  7595.  
  7596. <pre class="example"><pre class="verbatim">     
  7597.      To: "Hans Dermot Doran" <htd@ibhdoran.com>
  7598.      Subject: Re: flex problem
  7599.      In-reply-to: Your message of Wed, 15 Jul 1998 21:30:13 PDT.
  7600.      Date: Tue, 21 Jul 1998 14:23:34 PDT
  7601.      From: Vern Paxson <vern>
  7602.      
  7603.      > To overcome this, I gets() the stdin into a string and lex the string. The
  7604.      > string is lexed OK except that the end of string isn't lexed properly
  7605.      > (yy_scan_string()), that is the lexer dosn't recognise the end of string.
  7606.      
  7607.      Flex doesn't contain mechanisms for recognizing buffer endpoints.  But if
  7608.      you use fgets instead (which you should anyway, to protect against buffer
  7609.      overflows), then the final \n will be preserved in the string, and you can
  7610.      scan that in order to find the end of the string.
  7611.      
  7612.              Vern
  7613. </pre>
  7614. </pre>
  7615. <!-- TODO: Evaluate this faq. -->
  7616. <div class="node">
  7617. <p><hr>
  7618. <a name="unnamed-faq-78"></a>
  7619. <a name="unnamed_002dfaq_002d78"></a>
  7620. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d79">unnamed-faq-79</a>,
  7621. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d77">unnamed-faq-77</a>,
  7622. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7623.  
  7624. </div>
  7625.  
  7626. <h3 class="unnumberedsec">unnamed-faq-78</h3>
  7627.  
  7628. <pre class="example"><pre class="verbatim">     
  7629.      To: soumen@almaden.ibm.com
  7630.      Subject: Re: Flex++ 2.5.3 instance member vs. static member
  7631.      In-reply-to: Your message of Mon, 27 Jul 1998 02:10:04 PDT.
  7632.      Date: Tue, 28 Jul 1998 01:10:34 PDT
  7633.      From: Vern Paxson <vern>
  7634.      
  7635.      > %{
  7636.      > int mylineno = 0;
  7637.      > %}
  7638.      > ws      [ \t]+
  7639.      > alpha   [A-Za-z]
  7640.      > dig     [0-9]
  7641.      > %%
  7642.      >
  7643.      > Now you'd expect mylineno to be a member of each instance of class
  7644.      > yyFlexLexer, but is this the case?  A look at the lex.yy.cc file seems to
  7645.      > indicate otherwise; unless I am missing something the declaration of
  7646.      > mylineno seems to be outside any class scope.
  7647.      >
  7648.      > How will this work if I want to run a multi-threaded application with each
  7649.      > thread creating a FlexLexer instance?
  7650.      
  7651.      Derive your own subclass and make mylineno a member variable of it.
  7652.      
  7653.              Vern
  7654. </pre>
  7655. </pre>
  7656. <!-- TODO: Evaluate this faq. -->
  7657. <div class="node">
  7658. <p><hr>
  7659. <a name="unnamed-faq-79"></a>
  7660. <a name="unnamed_002dfaq_002d79"></a>
  7661. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d80">unnamed-faq-80</a>,
  7662. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d78">unnamed-faq-78</a>,
  7663. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7664.  
  7665. </div>
  7666.  
  7667. <h3 class="unnumberedsec">unnamed-faq-79</h3>
  7668.  
  7669. <pre class="example"><pre class="verbatim">     
  7670.      To: Adoram Rogel <adoram@hybridge.com>
  7671.      Subject: Re: More than 32K states change hangs
  7672.      In-reply-to: Your message of Tue, 04 Aug 1998 16:55:39 PDT.
  7673.      Date: Tue, 04 Aug 1998 22:28:45 PDT
  7674.      From: Vern Paxson <vern>
  7675.      
  7676.      > Vern Paxson,
  7677.      >
  7678.      > I followed your advice, posted on Usenet bu you, and emailed to me
  7679.      > personally by you, on how to overcome the 32K states limit. I'm running
  7680.      > on Linux machines.
  7681.      > I took the full source of version 2.5.4 and did the following changes in
  7682.      > flexdef.h:
  7683.      > #define JAMSTATE -327660
  7684.      > #define MAXIMUM_MNS 319990
  7685.      > #define BAD_SUBSCRIPT -327670
  7686.      > #define MAX_SHORT 327000
  7687.      >
  7688.      > and compiled.
  7689.      > All looked fine, including check and bigcheck, so I installed.
  7690.      
  7691.      Hmmm, you shouldn't increase MAX_SHORT, though looking through my email
  7692.      archives I see that I did indeed recommend doing so.  Try setting it back
  7693.      to 32700; that should suffice that you no longer need -Ca.  If it still
  7694.      hangs, then the interesting question is - where?
  7695.      
  7696.      > Compiling the same hanged program with a out-of-the-box (RedHat 4.2
  7697.      > distribution of Linux)
  7698.      > flex 2.5.4 binary works.
  7699.      
  7700.      Since Linux comes with source code, you should diff it against what
  7701.      you have to see what problems they missed.
  7702.      
  7703.      > Should I always compile with the -Ca option now ? even short and simple
  7704.      > filters ?
  7705.      
  7706.      No, definitely not.  It's meant to be for those situations where you
  7707.      absolutely must squeeze every last cycle out of your scanner.
  7708.      
  7709.              Vern
  7710. </pre>
  7711. </pre>
  7712. <!-- TODO: Evaluate this faq. -->
  7713. <div class="node">
  7714. <p><hr>
  7715. <a name="unnamed-faq-80"></a>
  7716. <a name="unnamed_002dfaq_002d80"></a>
  7717. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d81">unnamed-faq-81</a>,
  7718. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d79">unnamed-faq-79</a>,
  7719. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7720.  
  7721. </div>
  7722.  
  7723. <h3 class="unnumberedsec">unnamed-faq-80</h3>
  7724.  
  7725. <pre class="example"><pre class="verbatim">     
  7726.      To: "Schmackpfeffer, Craig" <Craig.Schmackpfeffer@usa.xerox.com>
  7727.      Subject: Re: flex output for static code portion
  7728.      In-reply-to: Your message of Tue, 11 Aug 1998 11:55:30 PDT.
  7729.      Date: Mon, 17 Aug 1998 23:57:42 PDT
  7730.      From: Vern Paxson <vern>
  7731.      
  7732.      > I would like to use flex under the hood to generate a binary file
  7733.      > containing the data structures that control the parse.
  7734.      
  7735.      This has been on the wish-list for a long time.  In principle it's
  7736.      straight-forward - you redirect mkdata() et al's I/O to another file,
  7737.      and modify the skeleton to have a start-up function that slurps these
  7738.      into dynamic arrays.  The concerns are (1) the scanner generation code
  7739.      is hairy and full of corner cases, so it's easy to get surprised when
  7740.      going down this path :-( ; and (2) being careful about buffering so
  7741.      that when the tables change you make sure the scanner starts in the
  7742.      correct state and reading at the right point in the input file.
  7743.      
  7744.      > I was wondering if you know of anyone who has used flex in this way.
  7745.      
  7746.      I don't - but it seems like a reasonable project to undertake (unlike
  7747.      numerous other flex tweaks :-).
  7748.      
  7749.              Vern
  7750. </pre>
  7751. </pre>
  7752. <!-- TODO: Evaluate this faq. -->
  7753. <div class="node">
  7754. <p><hr>
  7755. <a name="unnamed-faq-81"></a>
  7756. <a name="unnamed_002dfaq_002d81"></a>
  7757. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d82">unnamed-faq-82</a>,
  7758. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d80">unnamed-faq-80</a>,
  7759. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7760.  
  7761. </div>
  7762.  
  7763. <h3 class="unnumberedsec">unnamed-faq-81</h3>
  7764.  
  7765. <pre class="example"><pre class="verbatim">     
  7766.      Received: from 131.173.17.11 (131.173.17.11 [131.173.17.11])
  7767.          by ee.lbl.gov (8.9.1/8.9.1) with ESMTP id AAA03838
  7768.          for <vern@ee.lbl.gov>; Thu, 20 Aug 1998 00:47:57 -0700 (PDT)
  7769.      Received: from hal.cl-ki.uni-osnabrueck.de (hal.cl-ki.Uni-Osnabrueck.DE [131.173.141.2])
  7770.          by deimos.rz.uni-osnabrueck.de (8.8.7/8.8.8) with ESMTP id JAA34694
  7771.          for <vern@ee.lbl.gov>; Thu, 20 Aug 1998 09:47:55 +0200
  7772.      Received: (from georg@localhost) by hal.cl-ki.uni-osnabrueck.de (8.6.12/8.6.12) id JAA34834 for vern@ee.lbl.gov; Thu, 20 Aug 1998 09:47:54 +0200
  7773.      From: Georg Rehm <georg@hal.cl-ki.uni-osnabrueck.de>
  7774.      Message-Id: <199808200747.JAA34834@hal.cl-ki.uni-osnabrueck.de>
  7775.      Subject: "flex scanner push-back overflow"
  7776.      To: vern@ee.lbl.gov
  7777.      Date: Thu, 20 Aug 1998 09:47:54 +0200 (MEST)
  7778.      Reply-To: Georg.Rehm@CL-KI.Uni-Osnabrueck.DE
  7779.      X-NoJunk: Do NOT send commercial mail, spam or ads to this address!
  7780.      X-URL: http://www.cl-ki.uni-osnabrueck.de/~georg/
  7781.      X-Mailer: ELM [version 2.4ME+ PL28 (25)]
  7782.      MIME-Version: 1.0
  7783.      Content-Type: text/plain; charset=US-ASCII
  7784.      Content-Transfer-Encoding: 7bit
  7785.      
  7786.      Hi Vern,
  7787.      
  7788.      Yesterday, I encountered a strange problem: I use the macro processor m4
  7789.      to include some lengthy lists into a .l file. Following is a flex macro
  7790.      definition that causes some serious pain in my neck:
  7791.      
  7792.      AUTHOR           ("A. Boucard / L. Boucard"|"A. Dastarac / M. Levent"|"A.Boucaud / L.Boucaud"|"Abderrahim Lamchichi"|"Achmat Dangor"|"Adeline Toullier"|"Adewale Maja-Pearce"|"Ahmed Ziri"|"Akram Ellyas"|"Alain Bihr"|"Alain Gresh"|"Alain Guillemoles"|"Alain Joxe"|"Alain Morice"|"Alain Renon"|"Alain Zecchini"|"Albert Memmi"|"Alberto Manguel"|"Alex De Waal"|"Alfonso Artico"| [...])
  7793.      
  7794.      The complete list contains about 10kB. When I try to "flex" this file
  7795.      (on a Solaris 2.6 machine, using a modified flex 2.5.4 (I only increased
  7796.      some of the predefined values in flexdefs.h) I get the error:
  7797.      
  7798.      myflex/flex -8  sentag.tmp.l
  7799.      flex scanner push-back overflow
  7800.      
  7801.      When I remove the slashes in the macro definition everything works fine.
  7802.      As I understand it, the double quotes escape the slash-character so it
  7803.      really means "/" and not "trailing context". Furthermore, I tried to
  7804.      escape the slashes with backslashes, but with no use, the same error message
  7805.      appeared when flexing the code.
  7806.      
  7807.      Do you have an idea what's going on here?
  7808.      
  7809.      Greetings from Germany,
  7810.          Georg
  7811.      --
  7812.      Georg Rehm                                     georg@cl-ki.uni-osnabrueck.de
  7813.      Institute for Semantic Information Processing, University of Osnabrueck, FRG
  7814. </pre>
  7815. </pre>
  7816. <!-- TODO: Evaluate this faq. -->
  7817. <div class="node">
  7818. <p><hr>
  7819. <a name="unnamed-faq-82"></a>
  7820. <a name="unnamed_002dfaq_002d82"></a>
  7821. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d83">unnamed-faq-83</a>,
  7822. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d81">unnamed-faq-81</a>,
  7823. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7824.  
  7825. </div>
  7826.  
  7827. <h3 class="unnumberedsec">unnamed-faq-82</h3>
  7828.  
  7829. <pre class="example"><pre class="verbatim">     
  7830.      To: Georg.Rehm@CL-KI.Uni-Osnabrueck.DE
  7831.      Subject: Re: "flex scanner push-back overflow"
  7832.      In-reply-to: Your message of Thu, 20 Aug 1998 09:47:54 PDT.
  7833.      Date: Thu, 20 Aug 1998 07:05:35 PDT
  7834.      From: Vern Paxson <vern>
  7835.      
  7836.      > myflex/flex -8  sentag.tmp.l
  7837.      > flex scanner push-back overflow
  7838.      
  7839.      Flex itself uses a flex scanner.  That scanner is running out of buffer
  7840.      space when it tries to unput() the humongous macro you've defined.  When
  7841.      you remove the '/'s, you make it small enough so that it fits in the buffer;
  7842.      removing spaces would do the same thing.
  7843.      
  7844.      The fix is to either rethink how come you're using such a big macro and
  7845.      perhaps there's another/better way to do it; or to rebuild flex's own
  7846.      scan.c with a larger value for
  7847.      
  7848.          #define YY_BUF_SIZE 16384
  7849.      
  7850.      - Vern
  7851. </pre>
  7852. </pre>
  7853. <!-- TODO: Evaluate this faq. -->
  7854. <div class="node">
  7855. <p><hr>
  7856. <a name="unnamed-faq-83"></a>
  7857. <a name="unnamed_002dfaq_002d83"></a>
  7858. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d84">unnamed-faq-84</a>,
  7859. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d82">unnamed-faq-82</a>,
  7860. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7861.  
  7862. </div>
  7863.  
  7864. <h3 class="unnumberedsec">unnamed-faq-83</h3>
  7865.  
  7866. <pre class="example"><pre class="verbatim">     
  7867.      To: Jan Kort <jan@research.techforce.nl>
  7868.      Subject: Re: Flex
  7869.      In-reply-to: Your message of Fri, 04 Sep 1998 12:18:43 +0200.
  7870.      Date: Sat, 05 Sep 1998 00:59:49 PDT
  7871.      From: Vern Paxson <vern>
  7872.      
  7873.      > %%
  7874.      >
  7875.      > "TEST1\n"       { fprintf(stderr, "TEST1\n"); yyless(5); }
  7876.      > ^\n             { fprintf(stderr, "empty line\n"); }
  7877.      > .               { }
  7878.      > \n              { fprintf(stderr, "new line\n"); }
  7879.      >
  7880.      > %%
  7881.      > -- input ---------------------------------------
  7882.      > TEST1
  7883.      > -- output --------------------------------------
  7884.      > TEST1
  7885.      > empty line
  7886.      > ------------------------------------------------
  7887.      
  7888.      IMHO, it's not clear whether or not this is in fact a bug.  It depends
  7889.      on whether you view yyless() as backing up in the input stream, or as
  7890.      pushing new characters onto the beginning of the input stream.  Flex
  7891.      interprets it as the latter (for implementation convenience, I'll admit),
  7892.      and so considers the newline as in fact matching at the beginning of a
  7893.      line, as after all the last token scanned an entire line and so the
  7894.      scanner is now at the beginning of a new line.
  7895.      
  7896.      I agree that this is counter-intuitive for yyless(), given its
  7897.      functional description (it's less so for unput(), depending on whether
  7898.      you're unput()'ing new text or scanned text).  But I don't plan to
  7899.      change it any time soon, as it's a pain to do so.  Consequently,
  7900.      you do indeed need to use yy_set_bol() and YY_AT_BOL() to tweak
  7901.      your scanner into the behavior you desire.
  7902.      
  7903.      Sorry for the less-than-completely-satisfactory answer.
  7904.      
  7905.              Vern
  7906. </pre>
  7907. </pre>
  7908. <!-- TODO: Evaluate this faq. -->
  7909. <div class="node">
  7910. <p><hr>
  7911. <a name="unnamed-faq-84"></a>
  7912. <a name="unnamed_002dfaq_002d84"></a>
  7913. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d85">unnamed-faq-85</a>,
  7914. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d83">unnamed-faq-83</a>,
  7915. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7916.  
  7917. </div>
  7918.  
  7919. <h3 class="unnumberedsec">unnamed-faq-84</h3>
  7920.  
  7921. <pre class="example"><pre class="verbatim">     
  7922.      To: Patrick Krusenotto <krusenot@mac-info-link.de>
  7923.      Subject: Re: Problems with restarting flex-2.5.2-generated scanner
  7924.      In-reply-to: Your message of Thu, 24 Sep 1998 10:14:07 PDT.
  7925.      Date: Thu, 24 Sep 1998 23:28:43 PDT
  7926.      From: Vern Paxson <vern>
  7927.      
  7928.      > I am using flex-2.5.2 and bison 1.25 for Solaris and I am desperately
  7929.      > trying to make my scanner restart with a new file after my parser stops
  7930.      > with a parse error. When my compiler restarts, the parser always
  7931.      > receives the token after the token (in the old file!) that caused the
  7932.      > parser error.
  7933.      
  7934.      I suspect the problem is that your parser has read ahead in order
  7935.      to attempt to resolve an ambiguity, and when it's restarted it picks
  7936.      up with that token rather than reading a fresh one.  If you're using
  7937.      yacc, then the special "error" production can sometimes be used to
  7938.      consume tokens in an attempt to get the parser into a consistent state.
  7939.      
  7940.              Vern
  7941. </pre>
  7942. </pre>
  7943. <!-- TODO: Evaluate this faq. -->
  7944. <div class="node">
  7945. <p><hr>
  7946. <a name="unnamed-faq-85"></a>
  7947. <a name="unnamed_002dfaq_002d85"></a>
  7948. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d86">unnamed-faq-86</a>,
  7949. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d84">unnamed-faq-84</a>,
  7950. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7951.  
  7952. </div>
  7953.  
  7954. <h3 class="unnumberedsec">unnamed-faq-85</h3>
  7955.  
  7956. <pre class="example"><pre class="verbatim">     
  7957.      To: Henric Jungheim <junghelh@pe-nelson.com>
  7958.      Subject: Re: flex 2.5.4a
  7959.      In-reply-to: Your message of Tue, 27 Oct 1998 16:41:42 PST.
  7960.      Date: Tue, 27 Oct 1998 16:50:14 PST
  7961.      From: Vern Paxson <vern>
  7962.      
  7963.      > This brings up a feature request:  How about a command line
  7964.      > option to specify the filename when reading from stdin?  That way one
  7965.      > doesn't need to create a temporary file in order to get the "#line"
  7966.      > directives to make sense.
  7967.      
  7968.      Use -o combined with -t (per the man page description of -o).
  7969.      
  7970.      > P.S., Is there any simple way to use non-blocking IO to parse multiple
  7971.      > streams?
  7972.      
  7973.      Simple, no.
  7974.      
  7975.      One approach might be to return a magic character on EWOULDBLOCK and
  7976.      have a rule
  7977.      
  7978.          .*<magic-character>    // put back .*, eat magic character
  7979.      
  7980.      This is off the top of my head, not sure it'll work.
  7981.      
  7982.              Vern
  7983. </pre>
  7984. </pre>
  7985. <!-- TODO: Evaluate this faq. -->
  7986. <div class="node">
  7987. <p><hr>
  7988. <a name="unnamed-faq-86"></a>
  7989. <a name="unnamed_002dfaq_002d86"></a>
  7990. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d87">unnamed-faq-87</a>,
  7991. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d85">unnamed-faq-85</a>,
  7992. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  7993.  
  7994. </div>
  7995.  
  7996. <h3 class="unnumberedsec">unnamed-faq-86</h3>
  7997.  
  7998. <pre class="example"><pre class="verbatim">     
  7999.      To: "Repko, Billy D" <billy.d.repko@intel.com>
  8000.      Subject: Re: Compiling scanners
  8001.      In-reply-to: Your message of Wed, 13 Jan 1999 10:52:47 PST.
  8002.      Date: Thu, 14 Jan 1999 00:25:30 PST
  8003.      From: Vern Paxson <vern>
  8004.      
  8005.      > It appears that maybe it cannot find the lfl library.
  8006.      
  8007.      The Makefile in the distribution builds it, so you should have it.
  8008.      It's exceedingly trivial, just a main() that calls yylex() and
  8009.      a yyrap() that always returns 1.
  8010.      
  8011.      > %%
  8012.      >       \n      ++num_lines; ++num_chars;
  8013.      >       .       ++num_chars;
  8014.      
  8015.      You can't indent your rules like this - that's where the errors are coming
  8016.      from.  Flex copies indented text to the output file, it's how you do things
  8017.      like
  8018.      
  8019.          int num_lines_seen = 0;
  8020.      
  8021.      to declare local variables.
  8022.      
  8023.              Vern
  8024. </pre>
  8025. </pre>
  8026. <!-- TODO: Evaluate this faq. -->
  8027. <div class="node">
  8028. <p><hr>
  8029. <a name="unnamed-faq-87"></a>
  8030. <a name="unnamed_002dfaq_002d87"></a>
  8031. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d88">unnamed-faq-88</a>,
  8032. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d86">unnamed-faq-86</a>,
  8033. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8034.  
  8035. </div>
  8036.  
  8037. <h3 class="unnumberedsec">unnamed-faq-87</h3>
  8038.  
  8039. <pre class="example"><pre class="verbatim">     
  8040.      To: Erick Branderhorst <Erick.Branderhorst@asml.nl>
  8041.      Subject: Re: flex input buffer
  8042.      In-reply-to: Your message of Tue, 09 Feb 1999 13:53:46 PST.
  8043.      Date: Tue, 09 Feb 1999 21:03:37 PST
  8044.      From: Vern Paxson <vern>
  8045.      
  8046.      > In the flex.skl file the size of the default input buffers is set.  Can you
  8047.      > explain why this size is set and why it is such a high number.
  8048.      
  8049.      It's large to optimize performance when scanning large files.  You can
  8050.      safely make it a lot lower if needed.
  8051.      
  8052.              Vern
  8053. </pre>
  8054. </pre>
  8055. <!-- TODO: Evaluate this faq. -->
  8056. <div class="node">
  8057. <p><hr>
  8058. <a name="unnamed-faq-88"></a>
  8059. <a name="unnamed_002dfaq_002d88"></a>
  8060. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d90">unnamed-faq-90</a>,
  8061. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d87">unnamed-faq-87</a>,
  8062. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8063.  
  8064. </div>
  8065.  
  8066. <h3 class="unnumberedsec">unnamed-faq-88</h3>
  8067.  
  8068. <pre class="example"><pre class="verbatim">     
  8069.      To: "Guido Minnen" <guidomi@cogs.susx.ac.uk>
  8070.      Subject: Re: Flex error message
  8071.      In-reply-to: Your message of Wed, 24 Feb 1999 15:31:46 PST.
  8072.      Date: Thu, 25 Feb 1999 00:11:31 PST
  8073.      From: Vern Paxson <vern>
  8074.      
  8075.      > I'm extending a larger scanner written in Flex and I keep running into
  8076.      > problems. More specifically, I get the error message:
  8077.      > "flex: input rules are too complicated (>= 32000 NFA states)"
  8078.      
  8079.      Increase the definitions in flexdef.h for:
  8080.      
  8081.      #define JAMSTATE -32766 /* marks a reference to the state that always j
  8082.      ams */
  8083.      #define MAXIMUM_MNS 31999
  8084.      #define BAD_SUBSCRIPT -32767
  8085.      
  8086.      recompile everything, and it should all work.
  8087.      
  8088.              Vern
  8089. </pre>
  8090. </pre>
  8091. <!-- TODO: Evaluate this faq. -->
  8092. <div class="node">
  8093. <p><hr>
  8094. <a name="unnamed-faq-90"></a>
  8095. <a name="unnamed_002dfaq_002d90"></a>
  8096. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d91">unnamed-faq-91</a>,
  8097. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d88">unnamed-faq-88</a>,
  8098. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8099.  
  8100. </div>
  8101.  
  8102. <h3 class="unnumberedsec">unnamed-faq-90</h3>
  8103.  
  8104. <pre class="example"><pre class="verbatim">     
  8105.      To: "Dmitriy Goldobin" <gold@ems.chel.su>
  8106.      Subject: Re: FLEX trouble
  8107.      In-reply-to: Your message of Mon, 31 May 1999 18:44:49 PDT.
  8108.      Date: Tue, 01 Jun 1999 00:15:07 PDT
  8109.      From: Vern Paxson <vern>
  8110.      
  8111.      >   I have a trouble with FLEX. Why rule "/*".*"*/" work properly,=20
  8112.      > but rule "/*"(.|\n)*"*/" don't work ?
  8113.      
  8114.      The second of these will have to scan the entire input stream (because
  8115.      "(.|\n)*" matches an arbitrary amount of any text) in order to see if
  8116.      it ends with "*/", terminating the comment.  That potentially will overflow
  8117.      the input buffer.
  8118.      
  8119.      >   More complex rule "/*"([^*]|(\*/[^/]))*"*/ give an error
  8120.      > 'unrecognized rule'.
  8121.      
  8122.      You can't use the '/' operator inside parentheses.  It's not clear
  8123.      what "(a/b)*" actually means.
  8124.      
  8125.      >   I now use workaround with state <comment>, but single-rule is
  8126.      > better, i think.
  8127.      
  8128.      Single-rule is nice but will always have the problem of either setting
  8129.      restrictions on comments (like not allowing multi-line comments) and/or
  8130.      running the risk of consuming the entire input stream, as noted above.
  8131.      
  8132.              Vern
  8133. </pre>
  8134. </pre>
  8135. <!-- TODO: Evaluate this faq. -->
  8136. <div class="node">
  8137. <p><hr>
  8138. <a name="unnamed-faq-91"></a>
  8139. <a name="unnamed_002dfaq_002d91"></a>
  8140. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d92">unnamed-faq-92</a>,
  8141. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d90">unnamed-faq-90</a>,
  8142. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8143.  
  8144. </div>
  8145.  
  8146. <h3 class="unnumberedsec">unnamed-faq-91</h3>
  8147.  
  8148. <pre class="example"><pre class="verbatim">     
  8149.      Received: from mc-qout4.whowhere.com (mc-qout4.whowhere.com [209.185.123.18])
  8150.          by ee.lbl.gov (8.9.3/8.9.3) with SMTP id IAA05100
  8151.          for <vern@ee.lbl.gov>; Tue, 15 Jun 1999 08:56:06 -0700 (PDT)
  8152.      Received: from Unknown/Local ([?.?.?.?]) by my-deja.com; Tue Jun 15 08:55:43 1999
  8153.      To: vern@ee.lbl.gov
  8154.      Date: Tue, 15 Jun 1999 08:55:43 -0700
  8155.      From: "Aki Niimura" <neko@my-deja.com>
  8156.      Message-ID: <KNONDOHDOBGAEAAA@my-deja.com>
  8157.      Mime-Version: 1.0
  8158.      Cc:
  8159.      X-Sent-Mail: on
  8160.      Reply-To:
  8161.      X-Mailer: MailCity Service
  8162.      Subject: A question on flex C++ scanner
  8163.      X-Sender-Ip: 12.72.207.61
  8164.      Organization: My Deja Email  (http://www.my-deja.com:80)
  8165.      Content-Type: text/plain; charset=us-ascii
  8166.      Content-Transfer-Encoding: 7bit
  8167.      
  8168.      Dear Dr. Paxon,
  8169.      
  8170.      I have been using flex for years.
  8171.      It works very well on many projects.
  8172.      Most case, I used it to generate a scanner on C language.
  8173.      However, one project I needed to generate  a scanner
  8174.      on C++ lanuage. Thanks to your enhancement, flex did
  8175.      the job.
  8176.      
  8177.      Currently, I'm working on enhancing my previous project.
  8178.      I need to deal with multiple input streams (recursive
  8179.      inclusion) in this scanner (C++).
  8180.      I did similar thing for another scanner (C) as you
  8181.      explained in your documentation.
  8182.      
  8183.      The generated scanner (C++) has necessary methods:
  8184.      - switch_to_buffer(struct yy_buffer_state *b)
  8185.      - yy_create_buffer(istream *is, int sz)
  8186.      - yy_delete_buffer(struct yy_buffer_state *b)
  8187.      
  8188.      However, I couldn't figure out how to access current
  8189.      buffer (yy_current_buffer).
  8190.      
  8191.      yy_current_buffer is a protected member of yyFlexLexer.
  8192.      I can't access it directly.
  8193.      Then, I thought yy_create_buffer() with is = 0 might
  8194.      return current stream buffer. But it seems not as far
  8195.      as I checked the source. (flex 2.5.4)
  8196.      
  8197.      I went through the Web in addition to Flex documentation.
  8198.      However, it hasn't been successful, so far.
  8199.      
  8200.      It is not my intention to bother you, but, can you
  8201.      comment about how to obtain the current stream buffer?
  8202.      
  8203.      Your response would be highly appreciated.
  8204.      
  8205.      Best regards,
  8206.      Aki Niimura
  8207.      
  8208.      --== Sent via Deja.com http://www.deja.com/ ==--
  8209.      Share what you know. Learn what you don't.
  8210. </pre>
  8211. </pre>
  8212. <!-- TODO: Evaluate this faq. -->
  8213. <div class="node">
  8214. <p><hr>
  8215. <a name="unnamed-faq-92"></a>
  8216. <a name="unnamed_002dfaq_002d92"></a>
  8217. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d93">unnamed-faq-93</a>,
  8218. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d91">unnamed-faq-91</a>,
  8219. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8220.  
  8221. </div>
  8222.  
  8223. <h3 class="unnumberedsec">unnamed-faq-92</h3>
  8224.  
  8225. <pre class="example"><pre class="verbatim">     
  8226.      To: neko@my-deja.com
  8227.      Subject: Re: A question on flex C++ scanner
  8228.      In-reply-to: Your message of Tue, 15 Jun 1999 08:55:43 PDT.
  8229.      Date: Tue, 15 Jun 1999 09:04:24 PDT
  8230.      From: Vern Paxson <vern>
  8231.      
  8232.      > However, I couldn't figure out how to access current
  8233.      > buffer (yy_current_buffer).
  8234.      
  8235.      Derive your own subclass from yyFlexLexer.
  8236.      
  8237.              Vern
  8238. </pre>
  8239. </pre>
  8240. <!-- TODO: Evaluate this faq. -->
  8241. <div class="node">
  8242. <p><hr>
  8243. <a name="unnamed-faq-93"></a>
  8244. <a name="unnamed_002dfaq_002d93"></a>
  8245. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d94">unnamed-faq-94</a>,
  8246. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d92">unnamed-faq-92</a>,
  8247. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8248.  
  8249. </div>
  8250.  
  8251. <h3 class="unnumberedsec">unnamed-faq-93</h3>
  8252.  
  8253. <pre class="example"><pre class="verbatim">     
  8254.      To: "Stones, Darren" <Darren.Stones@nectech.co.uk>
  8255.      Subject: Re: You're the man to see?
  8256.      In-reply-to: Your message of Wed, 23 Jun 1999 11:10:29 PDT.
  8257.      Date: Wed, 23 Jun 1999 09:01:40 PDT
  8258.      From: Vern Paxson <vern>
  8259.      
  8260.      > I hope you can help me.  I am using Flex and Bison to produce an interpreted
  8261.      > language.  However all goes well until I try to implement an IF statement or
  8262.      > a WHILE.  I cannot get this to work as the parser parses all the conditions
  8263.      > eg. the TRUE and FALSE conditons to check for a rule match.  So I cannot
  8264.      > make a decision!!
  8265.      
  8266.      You need to use the parser to build a parse tree (= abstract syntax trwee),
  8267.      and when that's all done you recursively evaluate the tree, binding variables
  8268.      to values at that time.
  8269.      
  8270.              Vern
  8271. </pre>
  8272. </pre>
  8273. <!-- TODO: Evaluate this faq. -->
  8274. <div class="node">
  8275. <p><hr>
  8276. <a name="unnamed-faq-94"></a>
  8277. <a name="unnamed_002dfaq_002d94"></a>
  8278. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d95">unnamed-faq-95</a>,
  8279. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d93">unnamed-faq-93</a>,
  8280. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8281.  
  8282. </div>
  8283.  
  8284. <h3 class="unnumberedsec">unnamed-faq-94</h3>
  8285.  
  8286. <pre class="example"><pre class="verbatim">     
  8287.      To: Petr Danecek <petr@ics.cas.cz>
  8288.      Subject: Re: flex - question
  8289.      In-reply-to: Your message of Mon, 28 Jun 1999 19:21:41 PDT.
  8290.      Date: Fri, 02 Jul 1999 16:52:13 PDT
  8291.      From: Vern Paxson <vern>
  8292.      
  8293.      > file, it takes an enormous amount of time. It is funny, because the
  8294.      > source code has only 12 rules!!! I think it looks like an exponencial
  8295.      > growth.
  8296.      
  8297.      Right, that's the problem - some patterns (those with a lot of
  8298.      ambiguity, where yours has because at any given time the scanner can
  8299.      be in the middle of all sorts of combinations of the different
  8300.      rules) blow up exponentially.
  8301.      
  8302.      For your rules, there is an easy fix.  Change the ".*" that comes fater
  8303.      the directory name to "[^ ]*".  With that in place, the rules are no
  8304.      longer nearly so ambiguous, because then once one of the directories
  8305.      has been matched, no other can be matched (since they all require a
  8306.      leading blank).
  8307.      
  8308.      If that's not an acceptable solution, then you can enter a start state
  8309.      to pick up the .*\n after each directory is matched.
  8310.      
  8311.      Also note that for speed, you'll want to add a ".*" rule at the end,
  8312.      otherwise rules that don't match any of the patterns will be matched
  8313.      very slowly, a character at a time.
  8314.      
  8315.              Vern
  8316. </pre>
  8317. </pre>
  8318. <!-- TODO: Evaluate this faq. -->
  8319. <div class="node">
  8320. <p><hr>
  8321. <a name="unnamed-faq-95"></a>
  8322. <a name="unnamed_002dfaq_002d95"></a>
  8323. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d96">unnamed-faq-96</a>,
  8324. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d94">unnamed-faq-94</a>,
  8325. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8326.  
  8327. </div>
  8328.  
  8329. <h3 class="unnumberedsec">unnamed-faq-95</h3>
  8330.  
  8331. <pre class="example"><pre class="verbatim">     
  8332.      To: Tielman Koekemoer <tielman@spi.co.za>
  8333.      Subject: Re: Please help.
  8334.      In-reply-to: Your message of Thu, 08 Jul 1999 13:20:37 PDT.
  8335.      Date: Thu, 08 Jul 1999 08:20:39 PDT
  8336.      From: Vern Paxson <vern>
  8337.      
  8338.      > I was hoping you could help me with my problem.
  8339.      >
  8340.      > I tried compiling (gnu)flex on a Solaris 2.4 machine
  8341.      > but when I ran make (after configure) I got an error.
  8342.      >
  8343.      > --------------------------------------------------------------
  8344.      > gcc -c -I. -I. -g -O parse.c
  8345.      > ./flex -t -p  ./scan.l >scan.c
  8346.      > sh: ./flex: not found
  8347.      > *** Error code 1
  8348.      > make: Fatal error: Command failed for target `scan.c'
  8349.      > -------------------------------------------------------------
  8350.      >
  8351.      > What's strange to me is that I'm only
  8352.      > trying to install flex now. I then edited the Makefile to
  8353.      > and changed where it says "FLEX = flex" to "FLEX = lex"
  8354.      > ( lex: the native Solaris one ) but then it complains about
  8355.      > the "-p" option. Is there any way I can compile flex without
  8356.      > using flex or lex?
  8357.      >
  8358.      > Thanks so much for your time.
  8359.      
  8360.      You managed to step on the bootstrap sequence, which first copies
  8361.      initscan.c to scan.c in order to build flex.  Try fetching a fresh
  8362.      distribution from ftp.ee.lbl.gov.  (Or you can first try removing
  8363.      ".bootstrap" and doing a make again.)
  8364.      
  8365.              Vern
  8366. </pre>
  8367. </pre>
  8368. <!-- TODO: Evaluate this faq. -->
  8369. <div class="node">
  8370. <p><hr>
  8371. <a name="unnamed-faq-96"></a>
  8372. <a name="unnamed_002dfaq_002d96"></a>
  8373. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d97">unnamed-faq-97</a>,
  8374. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d95">unnamed-faq-95</a>,
  8375. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8376.  
  8377. </div>
  8378.  
  8379. <h3 class="unnumberedsec">unnamed-faq-96</h3>
  8380.  
  8381. <pre class="example"><pre class="verbatim">     
  8382.      To: Tielman Koekemoer <tielman@spi.co.za>
  8383.      Subject: Re: Please help.
  8384.      In-reply-to: Your message of Fri, 09 Jul 1999 09:16:14 PDT.
  8385.      Date: Fri, 09 Jul 1999 00:27:20 PDT
  8386.      From: Vern Paxson <vern>
  8387.      
  8388.      > First I removed .bootstrap (and ran make) - no luck. I downloaded the
  8389.      > software but I still have the same problem. Is there anything else I
  8390.      > could try.
  8391.      
  8392.      Try:
  8393.      
  8394.          cp initscan.c scan.c
  8395.          touch scan.c
  8396.          make scan.o
  8397.      
  8398.      If this last tries to first build scan.c from scan.l using ./flex, then
  8399.      your "make" is broken, in which case compile scan.c to scan.o by hand.
  8400.      
  8401.              Vern
  8402. </pre>
  8403. </pre>
  8404. <!-- TODO: Evaluate this faq. -->
  8405. <div class="node">
  8406. <p><hr>
  8407. <a name="unnamed-faq-97"></a>
  8408. <a name="unnamed_002dfaq_002d97"></a>
  8409. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d98">unnamed-faq-98</a>,
  8410. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d96">unnamed-faq-96</a>,
  8411. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8412.  
  8413. </div>
  8414.  
  8415. <h3 class="unnumberedsec">unnamed-faq-97</h3>
  8416.  
  8417. <pre class="example"><pre class="verbatim">     
  8418.      To: Sumanth Kamenani <skamenan@crl.nmsu.edu>
  8419.      Subject: Re: Error
  8420.      In-reply-to: Your message of Mon, 19 Jul 1999 23:08:41 PDT.
  8421.      Date: Tue, 20 Jul 1999 00:18:26 PDT
  8422.      From: Vern Paxson <vern>
  8423.      
  8424.      > I am getting a compilation error. The error is given as "unknown symbol- yylex".
  8425.      
  8426.      The parser relies on calling yylex(), but you're instead using the C++ scanning
  8427.      class, so you need to supply a yylex() "glue" function that calls an instance
  8428.      scanner of the scanner (e.g., "scanner->yylex()").
  8429.      
  8430.              Vern
  8431. </pre>
  8432. </pre>
  8433. <!-- TODO: Evaluate this faq. -->
  8434. <div class="node">
  8435. <p><hr>
  8436. <a name="unnamed-faq-98"></a>
  8437. <a name="unnamed_002dfaq_002d98"></a>
  8438. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d99">unnamed-faq-99</a>,
  8439. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d97">unnamed-faq-97</a>,
  8440. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8441.  
  8442. </div>
  8443.  
  8444. <h3 class="unnumberedsec">unnamed-faq-98</h3>
  8445.  
  8446. <pre class="example"><pre class="verbatim">     
  8447.      To: daniel@synchrods.synchrods.COM (Daniel Senderowicz)
  8448.      Subject: Re: lex
  8449.      In-reply-to: Your message of Mon, 22 Nov 1999 11:19:04 PST.
  8450.      Date: Tue, 23 Nov 1999 15:54:30 PST
  8451.      From: Vern Paxson <vern>
  8452.      
  8453.      Well, your problem is the
  8454.      
  8455.      switch (yybgin-yysvec-1) {      /* witchcraft */
  8456.      
  8457.      at the beginning of lex rules.  "witchcraft" == "non-portable".  It's
  8458.      assuming knowledge of the AT&T lex's internal variables.
  8459.      
  8460.      For flex, you can probably do the equivalent using a switch on YYSTATE.
  8461.      
  8462.              Vern
  8463. </pre>
  8464. </pre>
  8465. <!-- TODO: Evaluate this faq. -->
  8466. <div class="node">
  8467. <p><hr>
  8468. <a name="unnamed-faq-99"></a>
  8469. <a name="unnamed_002dfaq_002d99"></a>
  8470. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d100">unnamed-faq-100</a>,
  8471. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d98">unnamed-faq-98</a>,
  8472. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8473.  
  8474. </div>
  8475.  
  8476. <h3 class="unnumberedsec">unnamed-faq-99</h3>
  8477.  
  8478. <pre class="example"><pre class="verbatim">     
  8479.      To: archow@hss.hns.com
  8480.      Subject: Re: Regarding distribution of flex and yacc based grammars
  8481.      In-reply-to: Your message of Sun, 19 Dec 1999 17:50:24 +0530.
  8482.      Date: Wed, 22 Dec 1999 01:56:24 PST
  8483.      From: Vern Paxson <vern>
  8484.      
  8485.      > When we provide the customer with an object code distribution, is it
  8486.      > necessary for us to provide source
  8487.      > for the generated C files from flex and bison since they are generated by
  8488.      > flex and bison ?
  8489.      
  8490.      For flex, no.  I don't know what the current state of this is for bison.
  8491.      
  8492.      > Also, is there any requrirement for us to neccessarily  provide source for
  8493.      > the grammar files which are fed into flex and bison ?
  8494.      
  8495.      Again, for flex, no.
  8496.      
  8497.      See the file "COPYING" in the flex distribution for the legalese.
  8498.      
  8499.              Vern
  8500. </pre>
  8501. </pre>
  8502. <!-- TODO: Evaluate this faq. -->
  8503. <div class="node">
  8504. <p><hr>
  8505. <a name="unnamed-faq-100"></a>
  8506. <a name="unnamed_002dfaq_002d100"></a>
  8507. Next: <a rel="next" accesskey="n" href="#unnamed_002dfaq_002d101">unnamed-faq-101</a>,
  8508. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d99">unnamed-faq-99</a>,
  8509. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8510.  
  8511. </div>
  8512.  
  8513. <h3 class="unnumberedsec">unnamed-faq-100</h3>
  8514.  
  8515. <pre class="example"><pre class="verbatim">     
  8516.      To: Martin Gallwey <gallweym@hyperion.moe.ul.ie>
  8517.      Subject: Re: Flex, and self referencing rules
  8518.      In-reply-to: Your message of Sun, 20 Feb 2000 01:01:21 PST.
  8519.      Date: Sat, 19 Feb 2000 18:33:16 PST
  8520.      From: Vern Paxson <vern>
  8521.      
  8522.      > However, I do not use unput anywhere. I do use self-referencing
  8523.      > rules like this:
  8524.      >
  8525.      > UnaryExpr               ({UnionExpr})|("-"{UnaryExpr})
  8526.      
  8527.      You can't do this - flex is *not* a parser like yacc (which does indeed
  8528.      allow recursion), it is a scanner that's confined to regular expressions.
  8529.      
  8530.              Vern
  8531. </pre>
  8532. </pre>
  8533. <!-- TODO: Evaluate this faq. -->
  8534. <div class="node">
  8535. <p><hr>
  8536. <a name="unnamed-faq-101"></a>
  8537. <a name="unnamed_002dfaq_002d101"></a>
  8538. Previous: <a rel="previous" accesskey="p" href="#unnamed_002dfaq_002d100">unnamed-faq-100</a>,
  8539. Up: <a rel="up" accesskey="u" href="#FAQ">FAQ</a>
  8540.  
  8541. </div>
  8542.  
  8543. <h3 class="unnumberedsec">unnamed-faq-101</h3>
  8544.  
  8545. <pre class="example"><pre class="verbatim">     
  8546.      To: slg3@lehigh.edu (SAMUEL L. GULDEN)
  8547.      Subject: Re: Flex problem
  8548.      In-reply-to: Your message of Thu, 02 Mar 2000 12:29:04 PST.
  8549.      Date: Thu, 02 Mar 2000 23:00:46 PST
  8550.      From: Vern Paxson <vern>
  8551.      
  8552.      If this is exactly your program:
  8553.      
  8554.      > digit [0-9]
  8555.      > digits {digit}+
  8556.      > whitespace [ \t\n]+
  8557.      >
  8558.      > %%
  8559.      > "[" { printf("open_brac\n");}
  8560.      > "]" { printf("close_brac\n");}
  8561.      > "+" { printf("addop\n");}
  8562.      > "*" { printf("multop\n");}
  8563.      > {digits} { printf("NUMBER = %s\n", yytext);}
  8564.      > whitespace ;
  8565.      
  8566.      then the problem is that the last rule needs to be "{whitespace}" !
  8567.      
  8568.              Vern
  8569. </pre>
  8570. </pre>
  8571. <div class="node">
  8572. <p><hr>
  8573. <a name="Appendices"></a>
  8574. Next: <a rel="next" accesskey="n" href="#Indices">Indices</a>,
  8575. Previous: <a rel="previous" accesskey="p" href="#FAQ">FAQ</a>,
  8576. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  8577.  
  8578. </div>
  8579.  
  8580. <h2 class="appendix">Appendix A Appendices</h2>
  8581.  
  8582. <ul class="menu">
  8583. <li><a accesskey="1" href="#Makefiles-and-Flex">Makefiles and Flex</a>
  8584. <li><a accesskey="2" href="#Bison-Bridge">Bison Bridge</a>
  8585. <li><a accesskey="3" href="#M4-Dependency">M4 Dependency</a>
  8586. </ul>
  8587.  
  8588. <div class="node">
  8589. <p><hr>
  8590. <a name="Makefiles-and-Flex"></a>
  8591. Next: <a rel="next" accesskey="n" href="#Bison-Bridge">Bison Bridge</a>,
  8592. Previous: <a rel="previous" accesskey="p" href="#Appendices">Appendices</a>,
  8593. Up: <a rel="up" accesskey="u" href="#Appendices">Appendices</a>
  8594.  
  8595. </div>
  8596.  
  8597. <h3 class="appendixsec">A.1 Makefiles and Flex</h3>
  8598.  
  8599. <p><a name="index-Makefile_002c-syntax-487"></a>
  8600. In this appendix, we provide tips for writing Makefiles to build your scanners.
  8601.  
  8602.    <p>In a traditional build environment, we say that the <samp><span class="file">.c</span></samp> files are the
  8603. sources, and the <samp><span class="file">.o</span></samp> files are the intermediate files. When using
  8604. <code>flex</code>, however, the <samp><span class="file">.l</span></samp> files are the sources, and the generated
  8605. <samp><span class="file">.c</span></samp> files (along with the <samp><span class="file">.o</span></samp> files) are the intermediate files. 
  8606. This requires you to carefully plan your Makefile.
  8607.  
  8608.    <p>Modern <samp><span class="command">make</span></samp> programs understand that <samp><span class="file">foo.l</span></samp> is intended to
  8609. generate <samp><span class="file">lex.yy.c</span></samp> or <samp><span class="file">foo.c</span></samp>, and will behave
  8610. accordingly<a rel="footnote" href="#fn-4" name="fnd-4"><sup>4</sup></a>.  The
  8611. following Makefile does not explicitly instruct <samp><span class="command">make</span></samp> how to build
  8612. <samp><span class="file">foo.c</span></samp> from <samp><span class="file">foo.l</span></samp>. Instead, it relies on the implicit rules of the
  8613. <samp><span class="command">make</span></samp> program to build the intermediate file, <samp><span class="file">scan.c</span></samp>:
  8614.  
  8615.    <p><a name="index-Makefile_002c-example-of-implicit-rules-488"></a>
  8616. <pre class="example"><pre class="verbatim">     
  8617.          # Basic Makefile -- relies on implicit rules
  8618.          # Creates "myprogram" from "scan.l" and "myprogram.c"
  8619.          #
  8620.          LEX=flex
  8621.          myprogram: scan.o myprogram.o
  8622.          scan.o: scan.l
  8623.      
  8624. </pre>
  8625. </pre>
  8626.    <p>For simple cases, the above may be sufficient. For other cases,
  8627. you may have to explicitly instruct <samp><span class="command">make</span></samp> how to build your scanner. 
  8628. The following is an example of a Makefile containing explicit rules:
  8629.  
  8630.    <p><a name="index-Makefile_002c-explicit-example-489"></a>
  8631. <pre class="example"><pre class="verbatim">     
  8632.          # Basic Makefile -- provides explicit rules
  8633.          # Creates "myprogram" from "scan.l" and "myprogram.c"
  8634.          #
  8635.          LEX=flex
  8636.          myprogram: scan.o myprogram.o
  8637.                  $(CC) -o $@  $(LDFLAGS) $^
  8638.      
  8639.          myprogram.o: myprogram.c
  8640.                  $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^
  8641.      
  8642.          scan.o: scan.c
  8643.                  $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^
  8644.      
  8645.          scan.c: scan.l
  8646.                  $(LEX) $(LFLAGS) -o $@ $^
  8647.      
  8648.          clean:
  8649.                  $(RM) *.o scan.c
  8650.      
  8651. </pre>
  8652. </pre>
  8653.    <p>Notice in the above example that <samp><span class="file">scan.c</span></samp> is in the <code>clean</code> target. 
  8654. This is because we consider the file <samp><span class="file">scan.c</span></samp> to be an intermediate file.
  8655.  
  8656.    <p>Finally, we provide a realistic example of a <code>flex</code> scanner used with a
  8657. <code>bison</code> parser<a rel="footnote" href="#fn-5" name="fnd-5"><sup>5</sup></a>. 
  8658. There is a tricky problem we have to deal with. Since a <code>flex</code> scanner
  8659. will typically include a header file (e.g., <samp><span class="file">y.tab.h</span></samp>) generated by the
  8660. parser, we need to be sure that the header file is generated BEFORE the scanner
  8661. is compiled. We handle this case in the following example:
  8662.  
  8663. <pre class="example"><pre class="verbatim">     
  8664.          # Makefile example -- scanner and parser.
  8665.          # Creates "myprogram" from "scan.l", "parse.y", and "myprogram.c"
  8666.          #
  8667.          LEX     = flex
  8668.          YACC    = bison -y
  8669.          YFLAGS  = -d
  8670.          objects = scan.o parse.o myprogram.o
  8671.      
  8672.          myprogram: $(objects)
  8673.          scan.o: scan.l parse.c
  8674.          parse.o: parse.y
  8675.          myprogram.o: myprogram.c
  8676.      
  8677. </pre>
  8678. </pre>
  8679.    <p>In the above example, notice the line,
  8680.  
  8681. <pre class="example"><pre class="verbatim">     
  8682.          scan.o: scan.l parse.c
  8683. </pre>
  8684. </pre>
  8685.    <p>, which lists the file <samp><span class="file">parse.c</span></samp> (the generated parser) as a dependency of
  8686. <samp><span class="file">scan.o</span></samp>. We want to ensure that the parser is created before the scanner
  8687. is compiled, and the above line seems to do the trick. Feel free to experiment
  8688. with your specific implementation of <samp><span class="command">make</span></samp>.
  8689.  
  8690.    <p>For more details on writing Makefiles, see <a href="make.html#Top">Top</a>.
  8691.  
  8692. <div class="node">
  8693. <p><hr>
  8694. <a name="Bison-Bridge"></a>
  8695. Next: <a rel="next" accesskey="n" href="#M4-Dependency">M4 Dependency</a>,
  8696. Previous: <a rel="previous" accesskey="p" href="#Makefiles-and-Flex">Makefiles and Flex</a>,
  8697. Up: <a rel="up" accesskey="u" href="#Appendices">Appendices</a>
  8698.  
  8699. </div>
  8700.  
  8701. <h3 class="section">A.2 C Scanners with Bison Parsers</h3>
  8702.  
  8703. <p><a name="index-bison_002c-bridging-with-flex-490"></a><a name="index-yylval-491"></a><a name="index-yylloc-492"></a><a name="index-YYLTYPE-493"></a><a name="index-YYSTYPE-494"></a>
  8704. This section describes the <code>flex</code> features useful when integrating
  8705. <code>flex</code> with <code>GNU bison</code><a rel="footnote" href="#fn-6" name="fnd-6"><sup>6</sup></a>. 
  8706. Skip this section if you are not using
  8707. <code>bison</code> with your scanner.  Here we discuss only the <code>flex</code>
  8708. half of the <code>flex</code> and <code>bison</code> pair.  We do not discuss
  8709. <code>bison</code> in any detail.  For more information about generating
  8710. <code>bison</code> parsers, see <a href="bison.html#Top">Top</a>.
  8711.  
  8712.    <p>A compatible <code>bison</code> scanner is generated by declaring `<samp><span class="samp">%option
  8713. bison-bridge</span></samp>' or by supplying `<samp><span class="samp">--bison-bridge</span></samp>' when invoking <code>flex</code>
  8714. from the command line.  This instructs <code>flex</code> that the macro
  8715. <code>yylval</code> may be used. The data type for
  8716. <code>yylval</code>, <code>YYSTYPE</code>,
  8717. is typically defined in a header file, included in section 1 of the
  8718. <code>flex</code> input file.  For a list of functions and macros
  8719. available, See <a href="#bison_002dfunctions">bison-functions</a>.
  8720.  
  8721.    <p>The declaration of yylex becomes,
  8722.  
  8723.    <p><a name="index-yylex-_0028reentrant-version_0029-495"></a>
  8724. <pre class="example"><pre class="verbatim">     
  8725.            int yylex ( YYSTYPE * lvalp, yyscan_t scanner );
  8726. </pre>
  8727. </pre>
  8728.    <p>If <code>%option bison-locations</code> is specified, then the declaration
  8729. becomes,
  8730.  
  8731.    <p><a name="index-yylex-_0028reentrant-version_0029-496"></a>
  8732. <pre class="example"><pre class="verbatim">     
  8733.            int yylex ( YYSTYPE * lvalp, YYLTYPE * llocp, yyscan_t scanner );
  8734. </pre>
  8735. </pre>
  8736.    <p>Note that the macros <code>yylval</code> and <code>yylloc</code> evaluate to pointers. 
  8737. Support for <code>yylloc</code> is optional in <code>bison</code>, so it is optional in
  8738. <code>flex</code> as well. The following is an example of a <code>flex</code> scanner that
  8739. is compatible with <code>bison</code>.
  8740.  
  8741.    <p><a name="index-bison_002c-scanner-to-be-called-from-bison-497"></a>
  8742. <pre class="example"><pre class="verbatim">     
  8743.          /* Scanner for "C" assignment statements... sort of. */
  8744.          %{
  8745.          #include "y.tab.h"  /* Generated by bison. */
  8746.          %}
  8747.      
  8748.          %option bison-bridge bison-locations
  8749.          %
  8750.      
  8751.          [[:digit:]]+  { yylval->num = atoi(yytext);   return NUMBER;}
  8752.          [[:alnum:]]+  { yylval->str = strdup(yytext); return STRING;}
  8753.          "="|";"       { return yytext[0];}
  8754.          .  {}
  8755.          %
  8756. </pre>
  8757. </pre>
  8758.    <p>As you can see, there really is no magic here. We just use
  8759. <code>yylval</code> as we would any other variable. The data type of
  8760. <code>yylval</code> is generated by <code>bison</code>, and included in the file
  8761. <samp><span class="file">y.tab.h</span></samp>. Here is the corresponding <code>bison</code> parser:
  8762.  
  8763.    <p><a name="index-bison_002c-parser-498"></a>
  8764. <pre class="example"><pre class="verbatim">     
  8765.          /* Parser to convert "C" assignments to lisp. */
  8766.          %{
  8767.          /* Pass the argument to yyparse through to yylex. */
  8768.          #define YYPARSE_PARAM scanner
  8769.          #define YYLEX_PARAM   scanner
  8770.          %}
  8771.          %locations
  8772.          %pure_parser
  8773.          %union {
  8774.              int num;
  8775.              char* str;
  8776.          }
  8777.          %token <str> STRING
  8778.          %token <num> NUMBER
  8779.          %%
  8780.          assignment:
  8781.              STRING '=' NUMBER ';' {
  8782.                  printf( "(setf %s %d)", $1, $3 );
  8783.             }
  8784.          ;
  8785. </pre>
  8786. </pre>
  8787.    <div class="node">
  8788. <p><hr>
  8789. <a name="M4-Dependency"></a>
  8790. Previous: <a rel="previous" accesskey="p" href="#Bison-Bridge">Bison Bridge</a>,
  8791. Up: <a rel="up" accesskey="u" href="#Appendices">Appendices</a>
  8792.  
  8793. </div>
  8794.  
  8795. <h3 class="section">A.3 M4 Dependency</h3>
  8796.  
  8797. <p><a name="index-m4-499"></a>
  8798. The macro processor <code>m4</code><a rel="footnote" href="#fn-7" name="fnd-7"><sup>7</sup></a> must be installed wherever flex is installed. 
  8799. <code>flex</code> invokes `<samp><span class="samp">m4</span></samp>', found by searching the directories in the
  8800. <code>PATH</code> environment variable. Any code you place in section 1 or in the
  8801. actions will be sent through m4. Please follow these rules to protect your
  8802. code from unwanted <code>m4</code> processing.
  8803.  
  8804.      <ul>
  8805. <li>Do not use symbols that begin with, `<samp><span class="samp">m4_</span></samp>', such as, `<samp><span class="samp">m4_define</span></samp>',
  8806. or `<samp><span class="samp">m4_include</span></samp>', since those are reserved for <code>m4</code> macro names.
  8807.  
  8808.      <li>Do not use the strings `<samp><span class="samp">[[</span></samp>' or `<samp><span class="samp">]]</span></samp>' anywhere in your code. The
  8809. former is not valid in C, except within comments, but the latter is valid in
  8810. code such as <code>x[y[z]]</code>.
  8811.  
  8812.    </ul>
  8813.  
  8814.    <p><code>m4</code> is only required at the time you run <code>flex</code>. The generated
  8815. scanner is ordinary C or C++, and does <em>not</em> require <code>m4</code>.
  8816.  
  8817. <div class="node">
  8818. <p><hr>
  8819. <a name="Indices"></a>
  8820. Previous: <a rel="previous" accesskey="p" href="#Appendices">Appendices</a>,
  8821. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  8822.  
  8823. </div>
  8824.  
  8825. <h2 class="unnumbered">Indices</h2>
  8826.  
  8827. <ul class="menu">
  8828. <li><a accesskey="1" href="#Concept-Index">Concept Index</a>
  8829. <li><a accesskey="2" href="#Index-of-Functions-and-Macros">Index of Functions and Macros</a>
  8830. <li><a accesskey="3" href="#Index-of-Variables">Index of Variables</a>
  8831. <li><a accesskey="4" href="#Index-of-Data-Types">Index of Data Types</a>
  8832. <li><a accesskey="5" href="#Index-of-Hooks">Index of Hooks</a>
  8833. <li><a accesskey="6" href="#Index-of-Scanner-Options">Index of Scanner Options</a>
  8834. </ul>
  8835.  
  8836. <div class="node">
  8837. <p><hr>
  8838. <a name="Concept-Index"></a>
  8839. Next: <a rel="next" accesskey="n" href="#Index-of-Functions-and-Macros">Index of Functions and Macros</a>,
  8840. Previous: <a rel="previous" accesskey="p" href="#Indices">Indices</a>,
  8841. Up: <a rel="up" accesskey="u" href="#Indices">Indices</a>
  8842.  
  8843. </div>
  8844.  
  8845. <h3 class="unnumberedsec">Concept Index</h3>
  8846.  
  8847. <ul class="index-cp" compact>
  8848. <li><a href="#index-g_t_0024-as-normal-character-in-patterns-75">$ as normal character in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8849. <li><a href="#index-g_t_0025array_002c-advantages-of-94">%array, advantages of</a>: <a href="#Matching">Matching</a></li>
  8850. <li><a href="#index-g_t_0025array_002c-use-of-91">%array, use of</a>: <a href="#Matching">Matching</a></li>
  8851. <li><a href="#index-g_t_0025array_002c-with-C_002b_002b-95">%array, with C++</a>: <a href="#Matching">Matching</a></li>
  8852. <li><a href="#index-g_t_0025option-nowrap-155">%option nowrap</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  8853. <li><a href="#index-g_t_0025pointer_002c-and-unput_0028_0029-126">%pointer, and unput()</a>: <a href="#Actions">Actions</a></li>
  8854. <li><a href="#index-g_t_0025pointer_002c-use-of-92">%pointer, use of</a>: <a href="#Matching">Matching</a></li>
  8855. <li><a href="#index-g_t_0025top-27">%top</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8856. <li><a href="#index-g_t_0025_0040_007b-and-_0025_0040_007d_002c-in-Definitions-Section-24">%{ and %}, in Definitions Section</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8857. <li><a href="#index-g_t_0025_0040_007b-and-_0025_0040_007d_002c-in-Rules-Section-100">%{ and %}, in Rules Section</a>: <a href="#Actions">Actions</a></li>
  8858. <li><a href="#index-g_t_003c_003cEOF_003e_003e_002c-use-of-217"><<EOF>>, use of</a>: <a href="#EOF">EOF</a></li>
  8859. <li><a href="#index-g_t_005b_005d-in-patterns-40">[] in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8860. <li><a href="#index-g_t_005e-as-non_002dspecial-character-in-patterns-74">^ as non-special character in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8861. <li><a href="#index-accessor-functions_002c-use-of-426">accessor functions, use of</a>: <a href="#Accessor-Methods">Accessor Methods</a></li>
  8862. <li><a href="#index-actions-96">actions</a>: <a href="#Actions">Actions</a></li>
  8863. <li><a href="#index-actions_002c-embedded-C-strings-102">actions, embedded C strings</a>: <a href="#Actions">Actions</a></li>
  8864. <li><a href="#index-actions_002c-redefining-YY_005fBREAK-227">actions, redefining YY_BREAK</a>: <a href="#Misc-Macros">Misc Macros</a></li>
  8865. <li><a href="#index-actions_002c-use-of-_0040_007b-and-_0040_007d-101">actions, use of { and }</a>: <a href="#Actions">Actions</a></li>
  8866. <li><a href="#index-aliases_002c-how-to-define-18">aliases, how to define</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8867. <li><a href="#index-arguments_002c-command_002dline-245">arguments, command-line</a>: <a href="#Scanner-Options">Scanner Options</a></li>
  8868. <li><a href="#index-array_002c-default-size-for-yytext-231">array, default size for yytext</a>: <a href="#User-Values">User Values</a></li>
  8869. <li><a href="#index-backing-up_002c-eliminating-380">backing up, eliminating</a>: <a href="#Performance">Performance</a></li>
  8870. <li><a href="#index-backing-up_002c-eliminating-by-adding-error-rules-382">backing up, eliminating by adding error rules</a>: <a href="#Performance">Performance</a></li>
  8871. <li><a href="#index-backing-up_002c-eliminating-with-catch_002dall-rule-383">backing up, eliminating with catch-all rule</a>: <a href="#Performance">Performance</a></li>
  8872. <li><a href="#index-backing-up_002c-example-of-eliminating-379">backing up, example of eliminating</a>: <a href="#Performance">Performance</a></li>
  8873. <li><a href="#index-BEGIN-111">BEGIN</a>: <a href="#Actions">Actions</a></li>
  8874. <li><a href="#index-BEGIN_002c-explanation-167">BEGIN, explanation</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  8875. <li><a href="#index-beginning-of-line_002c-in-patterns-55">beginning of line, in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8876. <li><a href="#index-bison_002c-bridging-with-flex-490">bison, bridging with flex</a>: <a href="#Bison-Bridge">Bison Bridge</a></li>
  8877. <li><a href="#index-bison_002c-parser-498">bison, parser</a>: <a href="#Bison-Bridge">Bison Bridge</a></li>
  8878. <li><a href="#index-bison_002c-scanner-to-be-called-from-bison-497">bison, scanner to be called from bison</a>: <a href="#Bison-Bridge">Bison Bridge</a></li>
  8879. <li><a href="#index-BOL_002c-checking-the-BOL-flag-225">BOL, checking the BOL flag</a>: <a href="#Misc-Macros">Misc Macros</a></li>
  8880. <li><a href="#index-BOL_002c-in-patterns-56">BOL, in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8881. <li><a href="#index-BOL_002c-setting-it-223">BOL, setting it</a>: <a href="#Misc-Macros">Misc Macros</a></li>
  8882. <li><a href="#index-braces-in-patterns-46">braces in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8883. <li><a href="#index-bugs_002c-reporting-3">bugs, reporting</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  8884. <li><a href="#index-C-code-in-flex-input-26">C code in flex input</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8885. <li><a href="#index-C_002b_002b-391">C++</a>: <a href="#Cxx">Cxx</a></li>
  8886. <li><a href="#index-C_002b_002b-and-_0025array-233">C++ and %array</a>: <a href="#User-Values">User Values</a></li>
  8887. <li><a href="#index-C_002b_002b-I_002fO_002c-customizing-486">C++ I/O, customizing</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
  8888. <li><a href="#index-C_002b_002b-scanners_002c-including-multiple-scanners-412">C++ scanners, including multiple scanners</a>: <a href="#Cxx">Cxx</a></li>
  8889. <li><a href="#index-C_002b_002b-scanners_002c-use-of-408">C++ scanners, use of</a>: <a href="#Cxx">Cxx</a></li>
  8890. <li><a href="#index-c_002b_002b_002c-experimental-form-of-scanner-class-389">c++, experimental form of scanner class</a>: <a href="#Cxx">Cxx</a></li>
  8891. <li><a href="#index-C_002b_002b_002c-multiple-different-scanners-409">C++, multiple different scanners</a>: <a href="#Cxx">Cxx</a></li>
  8892. <li><a href="#index-C_002dstrings_002c-in-actions-103">C-strings, in actions</a>: <a href="#Actions">Actions</a></li>
  8893. <li><a href="#index-case_002dinsensitive_002c-effect-on-character-classes-70">case-insensitive, effect on character classes</a>: <a href="#Patterns">Patterns</a></li>
  8894. <li><a href="#index-character-classes-in-patterns-66">character classes in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8895. <li><a href="#index-character-classes-in-patterns_002c-syntax-of-41">character classes in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
  8896. <li><a href="#index-character-classes_002c-equivalence-of-68">character classes, equivalence of</a>: <a href="#Patterns">Patterns</a></li>
  8897. <li><a href="#index-clearing-an-input-buffer-201">clearing an input buffer</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  8898. <li><a href="#index-command_002dline-options-243">command-line options</a>: <a href="#Scanner-Options">Scanner Options</a></li>
  8899. <li><a href="#index-comments-in-flex-input-23">comments in flex input</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8900. <li><a href="#index-comments-in-the-input-35">comments in the input</a>: <a href="#Comments-in-the-Input">Comments in the Input</a></li>
  8901. <li><a href="#index-comments_002c-discarding-131">comments, discarding</a>: <a href="#Actions">Actions</a></li>
  8902. <li><a href="#index-comments_002c-example-of-scanning-C-comments-172">comments, example of scanning C comments</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  8903. <li><a href="#index-comments_002c-in-actions-104">comments, in actions</a>: <a href="#Actions">Actions</a></li>
  8904. <li><a href="#index-comments_002c-in-rules-section-33">comments, in rules section</a>: <a href="#Comments-in-the-Input">Comments in the Input</a></li>
  8905. <li><a href="#index-comments_002c-syntax-of-32">comments, syntax of</a>: <a href="#Comments-in-the-Input">Comments in the Input</a></li>
  8906. <li><a href="#index-comments_002c-valid-uses-of-34">comments, valid uses of</a>: <a href="#Comments-in-the-Input">Comments in the Input</a></li>
  8907. <li><a href="#index-compressing-whitespace-99">compressing whitespace</a>: <a href="#Actions">Actions</a></li>
  8908. <li><a href="#index-concatenation_002c-in-patterns-53">concatenation, in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8909. <li><a href="#index-copyright-of-flex-1">copyright of flex</a>: <a href="#Copyright">Copyright</a></li>
  8910. <li><a href="#index-counting-characters-and-lines-9">counting characters and lines</a>: <a href="#Simple-Examples">Simple Examples</a></li>
  8911. <li><a href="#index-customizing-I_002fO-in-C_002b_002b-scanners-485">customizing I/O in C++ scanners</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
  8912. <li><a href="#index-default-rule-88">default rule</a>: <a href="#Matching">Matching</a></li>
  8913. <li><a href="#index-default-rule-7">default rule</a>: <a href="#Simple-Examples">Simple Examples</a></li>
  8914. <li><a href="#index-defining-pattern-aliases-21">defining pattern aliases</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8915. <li><a href="#index-Definitions_002c-in-flex-input-17">Definitions, in flex input</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8916. <li><a href="#index-deleting-lines-from-input-97">deleting lines from input</a>: <a href="#Actions">Actions</a></li>
  8917. <li><a href="#index-discarding-C-comments-132">discarding C comments</a>: <a href="#Actions">Actions</a></li>
  8918. <li><a href="#index-distributing-flex-2">distributing flex</a>: <a href="#Copyright">Copyright</a></li>
  8919. <li><a href="#index-ECHO-110">ECHO</a>: <a href="#Actions">Actions</a></li>
  8920. <li><a href="#index-ECHO_002c-and-yyout-156">ECHO, and yyout</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  8921. <li><a href="#index-embedding-C-code-in-flex-input-25">embedding C code in flex input</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8922. <li><a href="#index-end-of-file_002c-in-patterns-61">end of file, in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8923. <li><a href="#index-end-of-line_002c-in-negated-character-classes-71">end of line, in negated character classes</a>: <a href="#Patterns">Patterns</a></li>
  8924. <li><a href="#index-end-of-line_002c-in-patterns-57">end of line, in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8925. <li><a href="#index-end_002dof_002dfile_002c-and-yyrestart_0028_0029-147">end-of-file, and yyrestart()</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  8926. <li><a href="#index-EOF-and-yyrestart_0028_0029-146">EOF and yyrestart()</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  8927. <li><a href="#index-EOF-in-patterns_002c-syntax-of-62">EOF in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
  8928. <li><a href="#index-EOF_002c-example-using-multiple-input-buffers-206">EOF, example using multiple input buffers</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  8929. <li><a href="#index-EOF_002c-explanation-215">EOF, explanation</a>: <a href="#EOF">EOF</a></li>
  8930. <li><a href="#index-EOF_002c-pushing-back-129">EOF, pushing back</a>: <a href="#Actions">Actions</a></li>
  8931. <li><a href="#index-EOL_002c-in-negated-character-classes-72">EOL, in negated character classes</a>: <a href="#Patterns">Patterns</a></li>
  8932. <li><a href="#index-EOL_002c-in-patterns-58">EOL, in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8933. <li><a href="#index-error-messages_002c-end-of-buffer-missed-449">error messages, end of buffer missed</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
  8934. <li><a href="#index-error-reporting_002c-diagnostic-messages-476">error reporting, diagnostic messages</a>: <a href="#Diagnostics">Diagnostics</a></li>
  8935. <li><a href="#index-error-reporting_002c-in-C_002b_002b-406">error reporting, in C++</a>: <a href="#Cxx">Cxx</a></li>
  8936. <li><a href="#index-error-rules_002c-to-eliminate-backing-up-381">error rules, to eliminate backing up</a>: <a href="#Performance">Performance</a></li>
  8937. <li><a href="#index-escape-sequences-in-patterns_002c-syntax-of-50">escape sequences in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
  8938. <li><a href="#index-exiting-with-yyterminate_0028_0029-139">exiting with yyterminate()</a>: <a href="#Actions">Actions</a></li>
  8939. <li><a href="#index-experimental-form-of-c_002b_002b-scanner-class-390">experimental form of c++ scanner class</a>: <a href="#Cxx">Cxx</a></li>
  8940. <li><a href="#index-extended-scope-of-start-conditions-181">extended scope of start conditions</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  8941. <li><a href="#index-file-format-13">file format</a>: <a href="#Format">Format</a></li>
  8942. <li><a href="#index-file-format_002c-serialized-tables-475">file format, serialized tables</a>: <a href="#Tables-File-Format">Tables File Format</a></li>
  8943. <li><a href="#index-flushing-an-input-buffer-202">flushing an input buffer</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  8944. <li><a href="#index-flushing-the-internal-buffer-135">flushing the internal buffer</a>: <a href="#Actions">Actions</a></li>
  8945. <li><a href="#index-format-of-flex-input-11">format of flex input</a>: <a href="#Format">Format</a></li>
  8946. <li><a href="#index-format-of-input-file-15">format of input file</a>: <a href="#Format">Format</a></li>
  8947. <li><a href="#index-freeing-tables-470">freeing tables</a>: <a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a></li>
  8948. <li><a href="#index-getting-current-start-state-with-YY_005fSTART-177">getting current start state with YY_START</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  8949. <li><a href="#index-halting-with-yyterminate_0028_0029-140">halting with yyterminate()</a>: <a href="#Actions">Actions</a></li>
  8950. <li><a href="#index-handling-include-files-with-multiple-input-buffers-207">handling include files with multiple input buffers</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  8951. <li><a href="#index-header-files_002c-with-C_002b_002b-411">header files, with C++</a>: <a href="#Cxx">Cxx</a></li>
  8952. <li><a href="#index-include-files_002c-with-C_002b_002b-410">include files, with C++</a>: <a href="#Cxx">Cxx</a></li>
  8953. <li><a href="#index-input-file_002c-Definitions-section-16">input file, Definitions section</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8954. <li><a href="#index-input-file_002c-Rules-Section-28">input file, Rules Section</a>: <a href="#Rules-Section">Rules Section</a></li>
  8955. <li><a href="#index-input-file_002c-user-code-Section-30">input file, user code Section</a>: <a href="#User-Code-Section">User Code Section</a></li>
  8956. <li><a href="#index-input_0028_0029-130">input()</a>: <a href="#Actions">Actions</a></li>
  8957. <li><a href="#index-input_0028_0029_002c-and-C_002b_002b-133">input(), and C++</a>: <a href="#Actions">Actions</a></li>
  8958. <li><a href="#index-input_002c-format-of-12">input, format of</a>: <a href="#Format">Format</a></li>
  8959. <li><a href="#index-input_002c-matching-80">input, matching</a>: <a href="#Matching">Matching</a></li>
  8960. <li><a href="#index-keywords_002c-for-performance-386">keywords, for performance</a>: <a href="#Performance">Performance</a></li>
  8961. <li><a href="#index-lex-_0028traditional_0029-and-POSIX-448">lex (traditional) and POSIX</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
  8962. <li><a href="#index-LexerInput_002c-overriding-482">LexerInput, overriding</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
  8963. <li><a href="#index-LexerOutput_002c-overriding-481">LexerOutput, overriding</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
  8964. <li><a href="#index-limitations-of-flex-479">limitations of flex</a>: <a href="#Limitations">Limitations</a></li>
  8965. <li><a href="#index-literal-text-in-patterns_002c-syntax-of-48">literal text in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
  8966. <li><a href="#index-loading-tables-at-runtime-468">loading tables at runtime</a>: <a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a></li>
  8967. <li><a href="#index-m4-499">m4</a>: <a href="#M4-Dependency">M4 Dependency</a></li>
  8968. <li><a href="#index-Makefile_002c-example-of-implicit-rules-488">Makefile, example of implicit rules</a>: <a href="#Makefiles-and-Flex">Makefiles and Flex</a></li>
  8969. <li><a href="#index-Makefile_002c-explicit-example-489">Makefile, explicit example</a>: <a href="#Makefiles-and-Flex">Makefiles and Flex</a></li>
  8970. <li><a href="#index-Makefile_002c-syntax-487">Makefile, syntax</a>: <a href="#Makefiles-and-Flex">Makefiles and Flex</a></li>
  8971. <li><a href="#index-matching-C_002dstyle-double_002dquoted-strings-179">matching C-style double-quoted strings</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  8972. <li><a href="#index-matching_002c-and-trailing-context-82">matching, and trailing context</a>: <a href="#Matching">Matching</a></li>
  8973. <li><a href="#index-matching_002c-length-of-83">matching, length of</a>: <a href="#Matching">Matching</a></li>
  8974. <li><a href="#index-matching_002c-multiple-matches-84">matching, multiple matches</a>: <a href="#Matching">Matching</a></li>
  8975. <li><a href="#index-member-functions_002c-C_002b_002b-392">member functions, C++</a>: <a href="#Cxx">Cxx</a></li>
  8976. <li><a href="#index-memory-management-455">memory management</a>: <a href="#Memory-Management">Memory Management</a></li>
  8977. <li><a href="#index-memory_002c-allocating-input-buffers-189">memory, allocating input buffers</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  8978. <li><a href="#index-memory_002c-considerations-for-reentrant-scanners-421">memory, considerations for reentrant scanners</a>: <a href="#Init-and-Destroy-Functions">Init and Destroy Functions</a></li>
  8979. <li><a href="#index-memory_002c-deleting-input-buffers-193">memory, deleting input buffers</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  8980. <li><a href="#index-memory_002c-for-start-condition-stacks-187">memory, for start condition stacks</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  8981. <li><a href="#index-memory_002c-serialized-tables-471">memory, serialized tables</a>: <a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a></li>
  8982. <li><a href="#index-memory_002c-serialized-tables-463">memory, serialized tables</a>: <a href="#Serialized-Tables">Serialized Tables</a></li>
  8983. <li><a href="#index-methods_002c-c_002b_002b-393">methods, c++</a>: <a href="#Cxx">Cxx</a></li>
  8984. <li><a href="#index-minimal-scanner-89">minimal scanner</a>: <a href="#Matching">Matching</a></li>
  8985. <li><a href="#index-multiple-input-streams-188">multiple input streams</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  8986. <li><a href="#index-name-definitions_002c-not-POSIX-451">name definitions, not POSIX</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
  8987. <li><a href="#index-negating-ranges-in-patterns-45">negating ranges in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8988. <li><a href="#index-newline_002c-matching-in-patterns-59">newline, matching in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8989. <li><a href="#index-non_002dPOSIX-features-of-flex-454">non-POSIX features of flex</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
  8990. <li><a href="#index-nowrap_002c-_0025option-154">nowrap, %option</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  8991. <li><a href="#index-NULL-character-in-patterns_002c-syntax-of-51">NULL character in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
  8992. <li><a href="#index-octal-characters-in-patterns-52">octal characters in patterns</a>: <a href="#Patterns">Patterns</a></li>
  8993. <li><a href="#index-options_002c-command_002dline-244">options, command-line</a>: <a href="#Scanner-Options">Scanner Options</a></li>
  8994. <li><a href="#index-overriding-LexerInput-484">overriding LexerInput</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
  8995. <li><a href="#index-overriding-LexerOutput-483">overriding LexerOutput</a>: <a href="#How-do-I-use-my-own-I_002fO-classes-in-a-C_002b_002b-scanner_003f">How do I use my own I/O classes in a C++ scanner?</a></li>
  8996. <li><a href="#index-overriding-the-memory-routines-460">overriding the memory routines</a>: <a href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a></li>
  8997. <li><a href="#index-Pascal_002dlike-language-10">Pascal-like language</a>: <a href="#Simple-Examples">Simple Examples</a></li>
  8998. <li><a href="#index-pattern-aliases_002c-defining-20">pattern aliases, defining</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  8999. <li><a href="#index-pattern-aliases_002c-expansion-of-47">pattern aliases, expansion of</a>: <a href="#Patterns">Patterns</a></li>
  9000. <li><a href="#index-pattern-aliases_002c-how-to-define-19">pattern aliases, how to define</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  9001. <li><a href="#index-pattern-aliases_002c-use-of-22">pattern aliases, use of</a>: <a href="#Definitions-Section">Definitions Section</a></li>
  9002. <li><a href="#index-patterns-and-actions-on-different-lines-452">patterns and actions on different lines</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
  9003. <li><a href="#index-patterns_002c-character-class-equivalence-69">patterns, character class equivalence</a>: <a href="#Patterns">Patterns</a></li>
  9004. <li><a href="#index-patterns_002c-end-of-line-78">patterns, end of line</a>: <a href="#Patterns">Patterns</a></li>
  9005. <li><a href="#index-patterns_002c-grouping-and-precedence-64">patterns, grouping and precedence</a>: <a href="#Patterns">Patterns</a></li>
  9006. <li><a href="#index-patterns_002c-in-rules-section-36">patterns, in rules section</a>: <a href="#Patterns">Patterns</a></li>
  9007. <li><a href="#index-patterns_002c-invalid-trailing-context-76">patterns, invalid trailing context</a>: <a href="#Patterns">Patterns</a></li>
  9008. <li><a href="#index-patterns_002c-matching-79">patterns, matching</a>: <a href="#Matching">Matching</a></li>
  9009. <li><a href="#index-patterns_002c-precedence-of-operators-63">patterns, precedence of operators</a>: <a href="#Patterns">Patterns</a></li>
  9010. <li><a href="#index-patterns_002c-repetitions-with-grouping-65">patterns, repetitions with grouping</a>: <a href="#Patterns">Patterns</a></li>
  9011. <li><a href="#index-patterns_002c-special-characters-treated-as-non_002dspecial-77">patterns, special characters treated as non-special</a>: <a href="#Patterns">Patterns</a></li>
  9012. <li><a href="#index-patterns_002c-syntax-38">patterns, syntax</a>: <a href="#Patterns">Patterns</a></li>
  9013. <li><a href="#index-patterns_002c-tuning-for-performance-377">patterns, tuning for performance</a>: <a href="#Performance">Performance</a></li>
  9014. <li><a href="#index-patterns_002c-valid-character-classes-67">patterns, valid character classes</a>: <a href="#Patterns">Patterns</a></li>
  9015. <li><a href="#index-performance-optimization_002c-matching-longer-tokens-385">performance optimization, matching longer tokens</a>: <a href="#Performance">Performance</a></li>
  9016. <li><a href="#index-performance-optimization_002c-recognizing-keywords-388">performance optimization, recognizing keywords</a>: <a href="#Performance">Performance</a></li>
  9017. <li><a href="#index-performance_002c-backing-up-378">performance, backing up</a>: <a href="#Performance">Performance</a></li>
  9018. <li><a href="#index-performance_002c-considerations-373">performance, considerations</a>: <a href="#Performance">Performance</a></li>
  9019. <li><a href="#index-performance_002c-using-keywords-387">performance, using keywords</a>: <a href="#Performance">Performance</a></li>
  9020. <li><a href="#index-popping-an-input-buffer-198">popping an input buffer</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9021. <li><a href="#index-POSIX-and-lex-447">POSIX and lex</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
  9022. <li><a href="#index-POSIX-comp_003bcompliance-453">POSIX comp;compliance</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
  9023. <li><a href="#index-POSIX_002c-character-classes-in-patterns_002c-syntax-of-42">POSIX, character classes in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
  9024. <li><a href="#index-preprocessor-macros_002c-for-use-in-actions-109">preprocessor macros, for use in actions</a>: <a href="#Actions">Actions</a></li>
  9025. <li><a href="#index-pushing-an-input-buffer-195">pushing an input buffer</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9026. <li><a href="#index-pushing-back-characters-with-unput-123">pushing back characters with unput</a>: <a href="#Actions">Actions</a></li>
  9027. <li><a href="#index-pushing-back-characters-with-unput_0028_0029-125">pushing back characters with unput()</a>: <a href="#Actions">Actions</a></li>
  9028. <li><a href="#index-pushing-back-characters-with-yyless-121">pushing back characters with yyless</a>: <a href="#Actions">Actions</a></li>
  9029. <li><a href="#index-pushing-back-EOF-128">pushing back EOF</a>: <a href="#Actions">Actions</a></li>
  9030. <li><a href="#index-ranges-in-patterns-43">ranges in patterns</a>: <a href="#Patterns">Patterns</a></li>
  9031. <li><a href="#index-ranges-in-patterns_002c-negating-44">ranges in patterns, negating</a>: <a href="#Patterns">Patterns</a></li>
  9032. <li><a href="#index-recognizing-C-comments-173">recognizing C comments</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9033. <li><a href="#index-reentrant-scanners_002c-multiple-interleaved-scanners-414">reentrant scanners, multiple interleaved scanners</a>: <a href="#Reentrant-Uses">Reentrant Uses</a></li>
  9034. <li><a href="#index-reentrant-scanners_002c-recursive-invocation-415">reentrant scanners, recursive invocation</a>: <a href="#Reentrant-Uses">Reentrant Uses</a></li>
  9035. <li><a href="#index-reentrant_002c-accessing-flex-variables-420">reentrant, accessing flex variables</a>: <a href="#Global-Replacement">Global Replacement</a></li>
  9036. <li><a href="#index-reentrant_002c-accessor-functions-425">reentrant, accessor functions</a>: <a href="#Accessor-Methods">Accessor Methods</a></li>
  9037. <li><a href="#index-reentrant_002c-API-explanation-416">reentrant, API explanation</a>: <a href="#Reentrant-Overview">Reentrant Overview</a></li>
  9038. <li><a href="#index-reentrant_002c-calling-functions-418">reentrant, calling functions</a>: <a href="#Extra-Reentrant-Argument">Extra Reentrant Argument</a></li>
  9039. <li><a href="#index-reentrant_002c-example-of-417">reentrant, example of</a>: <a href="#Reentrant-Example">Reentrant Example</a></li>
  9040. <li><a href="#index-reentrant_002c-explanation-413">reentrant, explanation</a>: <a href="#Reentrant">Reentrant</a></li>
  9041. <li><a href="#index-reentrant_002c-extra-data-427">reentrant, extra data</a>: <a href="#Extra-Data">Extra Data</a></li>
  9042. <li><a href="#index-reentrant_002c-initialization-422">reentrant, initialization</a>: <a href="#Init-and-Destroy-Functions">Init and Destroy Functions</a></li>
  9043. <li><a href="#index-regular-expressions_002c-in-patterns-37">regular expressions, in patterns</a>: <a href="#Patterns">Patterns</a></li>
  9044. <li><a href="#index-REJECT-112">REJECT</a>: <a href="#Actions">Actions</a></li>
  9045. <li><a href="#index-REJECT_002c-calling-multiple-times-113">REJECT, calling multiple times</a>: <a href="#Actions">Actions</a></li>
  9046. <li><a href="#index-REJECT_002c-performance-costs-374">REJECT, performance costs</a>: <a href="#Performance">Performance</a></li>
  9047. <li><a href="#index-reporting-bugs-4">reporting bugs</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  9048. <li><a href="#index-restarting-the-scanner-450">restarting the scanner</a>: <a href="#Lex-and-Posix">Lex and Posix</a></li>
  9049. <li><a href="#index-RETURN_002c-within-actions-149">RETURN, within actions</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9050. <li><a href="#index-rules_002c-default-8">rules, default</a>: <a href="#Simple-Examples">Simple Examples</a></li>
  9051. <li><a href="#index-rules_002c-in-flex-input-29">rules, in flex input</a>: <a href="#Rules-Section">Rules Section</a></li>
  9052. <li><a href="#index-scanner_002c-definition-of-5">scanner, definition of</a>: <a href="#Introduction">Introduction</a></li>
  9053. <li><a href="#index-sections-of-flex-input-14">sections of flex input</a>: <a href="#Format">Format</a></li>
  9054. <li><a href="#index-serialization-462">serialization</a>: <a href="#Serialized-Tables">Serialized Tables</a></li>
  9055. <li><a href="#index-serialization-of-tables-465">serialization of tables</a>: <a href="#Creating-Serialized-Tables">Creating Serialized Tables</a></li>
  9056. <li><a href="#index-serialized-tables_002c-multiple-scanners-466">serialized tables, multiple scanners</a>: <a href="#Creating-Serialized-Tables">Creating Serialized Tables</a></li>
  9057. <li><a href="#index-stack_002c-input-buffer-pop-199">stack, input buffer pop</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9058. <li><a href="#index-stack_002c-input-buffer-push-196">stack, input buffer push</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9059. <li><a href="#index-stacks_002c-routines-for-manipulating-182">stacks, routines for manipulating</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9060. <li><a href="#index-start-condition_002c-applying-to-multiple-patterns-180">start condition, applying to multiple patterns</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9061. <li><a href="#index-start-conditions-159">start conditions</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9062. <li><a href="#index-start-conditions_002c-behavior-of-default-rule-166">start conditions, behavior of default rule</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9063. <li><a href="#index-start-conditions_002c-exclusive-163">start conditions, exclusive</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9064. <li><a href="#index-start-conditions_002c-for-different-interpretations-of-same-input-171">start conditions, for different interpretations of same input</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9065. <li><a href="#index-start-conditions_002c-in-patterns-60">start conditions, in patterns</a>: <a href="#Patterns">Patterns</a></li>
  9066. <li><a href="#index-start-conditions_002c-inclusive-162">start conditions, inclusive</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9067. <li><a href="#index-start-conditions_002c-inclusive-v_002es_002e-exclusive-161">start conditions, inclusive v.s. exclusive</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9068. <li><a href="#index-start-conditions_002c-integer-values-174">start conditions, integer values</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9069. <li><a href="#index-start-conditions_002c-multiple-160">start conditions, multiple</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9070. <li><a href="#index-start-conditions_002c-special-wildcard-condition-164">start conditions, special wildcard condition</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9071. <li><a href="#index-start-conditions_002c-use-of-a-stack-183">start conditions, use of a stack</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9072. <li><a href="#index-start-conditions_002c-use-of-wildcard-condition-_0028_003c_002a_003e_0029-165">start conditions, use of wildcard condition (<*>)</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9073. <li><a href="#index-start-conditions_002c-using-BEGIN-170">start conditions, using BEGIN</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9074. <li><a href="#index-stdin_002c-default-for-yyin-144">stdin, default for yyin</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9075. <li><a href="#index-stdout_002c-as-default-for-yyout-158">stdout, as default for yyout</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9076. <li><a href="#index-strings_002c-scanning-strings-instead-of-files-209">strings, scanning strings instead of files</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9077. <li><a href="#index-tables_002c-creating-serialized-464">tables, creating serialized</a>: <a href="#Creating-Serialized-Tables">Creating Serialized Tables</a></li>
  9078. <li><a href="#index-tables_002c-file-format-474">tables, file format</a>: <a href="#Tables-File-Format">Tables File Format</a></li>
  9079. <li><a href="#index-tables_002c-freeing-469">tables, freeing</a>: <a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a></li>
  9080. <li><a href="#index-tables_002c-loading-and-unloading-467">tables, loading and unloading</a>: <a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a></li>
  9081. <li><a href="#index-terminating-with-yyterminate_0028_0029-138">terminating with yyterminate()</a>: <a href="#Actions">Actions</a></li>
  9082. <li><a href="#index-token-85">token</a>: <a href="#Matching">Matching</a></li>
  9083. <li><a href="#index-trailing-context_002c-in-patterns-54">trailing context, in patterns</a>: <a href="#Patterns">Patterns</a></li>
  9084. <li><a href="#index-trailing-context_002c-limits-of-73">trailing context, limits of</a>: <a href="#Patterns">Patterns</a></li>
  9085. <li><a href="#index-trailing-context_002c-matching-81">trailing context, matching</a>: <a href="#Matching">Matching</a></li>
  9086. <li><a href="#index-trailing-context_002c-performance-costs-376">trailing context, performance costs</a>: <a href="#Performance">Performance</a></li>
  9087. <li><a href="#index-trailing-context_002c-variable-length-384">trailing context, variable length</a>: <a href="#Performance">Performance</a></li>
  9088. <li><a href="#index-unput_0028_0029-122">unput()</a>: <a href="#Actions">Actions</a></li>
  9089. <li><a href="#index-unput_0028_0029_002c-and-_0025pointer-127">unput(), and %pointer</a>: <a href="#Actions">Actions</a></li>
  9090. <li><a href="#index-unput_0028_0029_002c-pushing-back-characters-124">unput(), pushing back characters</a>: <a href="#Actions">Actions</a></li>
  9091. <li><a href="#index-user-code_002c-in-flex-input-31">user code, in flex input</a>: <a href="#User-Code-Section">User Code Section</a></li>
  9092. <li><a href="#index-username-expansion-6">username expansion</a>: <a href="#Simple-Examples">Simple Examples</a></li>
  9093. <li><a href="#index-using-integer-values-of-start-condition-names-175">using integer values of start condition names</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9094. <li><a href="#index-verbatim-text-in-patterns_002c-syntax-of-49">verbatim text in patterns, syntax of</a>: <a href="#Patterns">Patterns</a></li>
  9095. <li><a href="#index-warning_002c-dangerous-trailing-context-480">warning, dangerous trailing context</a>: <a href="#Limitations">Limitations</a></li>
  9096. <li><a href="#index-warning_002c-rule-cannot-be-matched-478">warning, rule cannot be matched</a>: <a href="#Diagnostics">Diagnostics</a></li>
  9097. <li><a href="#index-warnings_002c-diagnostic-messages-477">warnings, diagnostic messages</a>: <a href="#Diagnostics">Diagnostics</a></li>
  9098. <li><a href="#index-whitespace_002c-compressing-98">whitespace, compressing</a>: <a href="#Actions">Actions</a></li>
  9099. <li><a href="#index-yacc-interface-242">yacc interface</a>: <a href="#Yacc">Yacc</a></li>
  9100. <li><a href="#index-yacc_002c-interface-240">yacc, interface</a>: <a href="#Yacc">Yacc</a></li>
  9101. <li><a href="#index-YY_005fCURRENT_005fBUFFER_002c-and-multiple-buffers-Finally_002c-the-macro-205">YY_CURRENT_BUFFER, and multiple buffers Finally, the macro</a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9102. <li><a href="#index-YY_005fEXTRA_005fTYPE_002c-defining-your-own-type-432">YY_EXTRA_TYPE, defining your own type</a>: <a href="#Extra-Data">Extra Data</a></li>
  9103. <li><a href="#index-YY_005fFLUSH_005fBUFFER_0028_0029-136">YY_FLUSH_BUFFER()</a>: <a href="#Actions">Actions</a></li>
  9104. <li><a href="#index-YY_005fINPUT-150">YY_INPUT</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9105. <li><a href="#index-YY_005fINPUT_002c-overriding-151">YY_INPUT, overriding</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9106. <li><a href="#index-YY_005fSTART_002c-example-176">YY_START, example</a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9107. <li><a href="#index-YY_005fUSER_005fACTION-to-track-each-time-a-rule-is-matched-219">YY_USER_ACTION to track each time a rule is matched</a>: <a href="#Misc-Macros">Misc Macros</a></li>
  9108. <li><a href="#index-yyalloc_002c-overriding-456">yyalloc, overriding</a>: <a href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a></li>
  9109. <li><a href="#index-yyfree_002c-overriding-458">yyfree, overriding</a>: <a href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a></li>
  9110. <li><a href="#index-yyin-145">yyin</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9111. <li><a href="#index-yyinput_0028_0029-134">yyinput()</a>: <a href="#Actions">Actions</a></li>
  9112. <li><a href="#index-yyleng-87">yyleng</a>: <a href="#Matching">Matching</a></li>
  9113. <li><a href="#index-yyleng_002c-modification-of-107">yyleng, modification of</a>: <a href="#Actions">Actions</a></li>
  9114. <li><a href="#index-yyless_0028_0029-119">yyless()</a>: <a href="#Actions">Actions</a></li>
  9115. <li><a href="#index-yyless_0028_0029_002c-pushing-back-characters-120">yyless(), pushing back characters</a>: <a href="#Actions">Actions</a></li>
  9116. <li><a href="#index-yylex_0028_0029_002c-in-generated-scanner-141">yylex(), in generated scanner</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9117. <li><a href="#index-yylex_0028_0029_002c-overriding-142">yylex(), overriding</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9118. <li><a href="#index-yylex_002c-overriding-the-prototype-of-143">yylex, overriding the prototype of</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9119. <li><a href="#index-yylineno_002c-in-a-reentrant-scanner-446">yylineno, in a reentrant scanner</a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9120. <li><a href="#index-yylineno_002c-performance-costs-375">yylineno, performance costs</a>: <a href="#Performance">Performance</a></li>
  9121. <li><a href="#index-yymore_0028_0029-115">yymore()</a>: <a href="#Actions">Actions</a></li>
  9122. <li><a href="#index-yymore_0028_0029-to-append-token-to-previous-token-117">yymore() to append token to previous token</a>: <a href="#Actions">Actions</a></li>
  9123. <li><a href="#index-yymore_0028_0029_002c-mega_002dkludge-116">yymore(), mega-kludge</a>: <a href="#Actions">Actions</a></li>
  9124. <li><a href="#index-yymore_002c-and-yyleng-108">yymore, and yyleng</a>: <a href="#Actions">Actions</a></li>
  9125. <li><a href="#index-yymore_002c-performance-penalty-of-118">yymore, performance penalty of</a>: <a href="#Actions">Actions</a></li>
  9126. <li><a href="#index-yyout-157">yyout</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9127. <li><a href="#index-yyrealloc_002c-overriding-457">yyrealloc, overriding</a>: <a href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a></li>
  9128. <li><a href="#index-yyrestart_0028_0029-148">yyrestart()</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9129. <li><a href="#index-yyterminate_0028_0029-137">yyterminate()</a>: <a href="#Actions">Actions</a></li>
  9130. <li><a href="#index-yytext-86">yytext</a>: <a href="#Matching">Matching</a></li>
  9131. <li><a href="#index-yytext_002c-default-array-size-230">yytext, default array size</a>: <a href="#User-Values">User Values</a></li>
  9132. <li><a href="#index-yytext_002c-memory-considerations-461">yytext, memory considerations</a>: <a href="#A-Note-About-yytext-And-Memory">A Note About yytext And Memory</a></li>
  9133. <li><a href="#index-yytext_002c-modification-of-106">yytext, modification of</a>: <a href="#Actions">Actions</a></li>
  9134. <li><a href="#index-yytext_002c-two-types-of-90">yytext, two types of</a>: <a href="#Matching">Matching</a></li>
  9135. <li><a href="#index-yywrap_0028_0029-152">yywrap()</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9136. <li><a href="#index-yywrap_002c-default-for-153">yywrap, default for</a>: <a href="#Generated-Scanner">Generated Scanner</a></li>
  9137. <li><a href="#index-g_t_007c_002c-in-actions-105">|, in actions</a>: <a href="#Actions">Actions</a></li>
  9138. <li><a href="#index-g_t_007c_002c-use-of-114">|, use of</a>: <a href="#Actions">Actions</a></li>
  9139.    </ul><div class="node">
  9140. <p><hr>
  9141. <a name="Index-of-Functions-and-Macros"></a>
  9142. Next: <a rel="next" accesskey="n" href="#Index-of-Variables">Index of Variables</a>,
  9143. Previous: <a rel="previous" accesskey="p" href="#Concept-Index">Concept Index</a>,
  9144. Up: <a rel="up" accesskey="u" href="#Indices">Indices</a>
  9145.  
  9146. </div>
  9147.  
  9148. <h3 class="unnumberedsec">Index of Functions and Macros</h3>
  9149.  
  9150. <p>This is an index of functions and preprocessor macros that look like functions. 
  9151. For macros that expand to variables or constants, see <a href="#Index-of-Variables">Index of Variables</a>.
  9152.  
  9153.  
  9154.  
  9155. <ul class="index-fn" compact>
  9156. <li><a href="#index-BEGIN-168"><code>BEGIN</code></a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9157. <li><a href="#index-debug-_0028C_002b_002b-only_0029-398"><code>debug (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9158. <li><a href="#index-LexerError-_0028C_002b_002b-only_0029-407"><code>LexerError (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9159. <li><a href="#index-LexerInput-_0028C_002b_002b-only_0029-404"><code>LexerInput (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9160. <li><a href="#index-LexerOutput-_0028C_002b_002b-only_0029-405"><code>LexerOutput (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9161. <li><a href="#index-lineno-_0028C_002b_002b-only_0029-396"><code>lineno (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9162. <li><a href="#index-set_005fdebug-_0028C_002b_002b-only_0029-397"><code>set_debug (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9163. <li><a href="#index-switch_005fstreams-_0028C_002b_002b-only_0029-403"><code>switch_streams (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9164. <li><a href="#index-YY_005fAT_005fBOL-226"><code>YY_AT_BOL</code></a>: <a href="#Misc-Macros">Misc Macros</a></li>
  9165. <li><a href="#index-yy_005fcreate_005fbuffer-190"><code>yy_create_buffer</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9166. <li><a href="#index-yy_005fdelete_005fbuffer-194"><code>yy_delete_buffer</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9167. <li><a href="#index-yy_005fflush_005fbuffer-203"><code>yy_flush_buffer</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9168. <li><a href="#index-yy_005fnew_005fbuffer-204"><code>yy_new_buffer</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9169. <li><a href="#index-YY_005fNEW_005fFILE--_0028now-obsolete_0029-216"><code>YY_NEW_FILE  (now obsolete)</code></a>: <a href="#EOF">EOF</a></li>
  9170. <li><a href="#index-yy_005fpop_005fstate-185"><code>yy_pop_state</code></a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9171. <li><a href="#index-yy_005fpush_005fstate-184"><code>yy_push_state</code></a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9172. <li><a href="#index-yy_005fscan_005fbuffer-213"><code>yy_scan_buffer</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9173. <li><a href="#index-yy_005fscan_005fbytes-211"><code>yy_scan_bytes</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9174. <li><a href="#index-yy_005fscan_005fstring-210"><code>yy_scan_string</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9175. <li><a href="#index-yy_005fset_005fbol-224"><code>yy_set_bol</code></a>: <a href="#Misc-Macros">Misc Macros</a></li>
  9176. <li><a href="#index-yy_005fset_005finteractive-222"><code>yy_set_interactive</code></a>: <a href="#Misc-Macros">Misc Macros</a></li>
  9177. <li><a href="#index-yy_005fswitch_005fto_005fbuffer-192"><code>yy_switch_to_buffer</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9178. <li><a href="#index-yy_005ftop_005fstate-186"><code>yy_top_state</code></a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9179. <li><a href="#index-yyFlexLexer-constructor-_0028C_002b_002b-only_0029-401"><code>yyFlexLexer constructor (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9180. <li><a href="#index-yyget_005fdebug-442"><code>yyget_debug</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9181. <li><a href="#index-yyget_005fextra-444"><code>yyget_extra</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9182. <li><a href="#index-yyget_005fextra-430"><code>yyget_extra</code></a>: <a href="#Extra-Data">Extra Data</a></li>
  9183. <li><a href="#index-yyget_005fin-436"><code>yyget_in</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9184. <li><a href="#index-yyget_005fleng-435"><code>yyget_leng</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9185. <li><a href="#index-yyget_005flineno-438"><code>yyget_lineno</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9186. <li><a href="#index-yyget_005fout-437"><code>yyget_out</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9187. <li><a href="#index-yyget_005ftext-434"><code>yyget_text</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9188. <li><a href="#index-YYLeng-_0028C_002b_002b-only_0029-395"><code>YYLeng (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9189. <li><a href="#index-yylex-_0028C_002b_002b-version_0029-402"><code>yylex (C++ version)</code></a>: <a href="#Cxx">Cxx</a></li>
  9190. <li><a href="#index-yylex-_0028reentrant-version_0029-495"><code>yylex (reentrant version)</code></a>: <a href="#Bison-Bridge">Bison Bridge</a></li>
  9191. <li><a href="#index-yylex_005fdestroy-424"><code>yylex_destroy</code></a>: <a href="#Init-and-Destroy-Functions">Init and Destroy Functions</a></li>
  9192. <li><a href="#index-yylex_005finit-423"><code>yylex_init</code></a>: <a href="#Init-and-Destroy-Functions">Init and Destroy Functions</a></li>
  9193. <li><a href="#index-yypop_005fbuffer_005fstate-200"><code>yypop_buffer_state</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9194. <li><a href="#index-yypush_005fbuffer_005fstate-197"><code>yypush_buffer_state</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9195. <li><a href="#index-yyrestart-236"><code>yyrestart</code></a>: <a href="#User-Values">User Values</a></li>
  9196. <li><a href="#index-yyset_005fdebug-443"><code>yyset_debug</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9197. <li><a href="#index-yyset_005fextra-445"><code>yyset_extra</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9198. <li><a href="#index-yyset_005fextra-431"><code>yyset_extra</code></a>: <a href="#Extra-Data">Extra Data</a></li>
  9199. <li><a href="#index-yyset_005fin-439"><code>yyset_in</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9200. <li><a href="#index-yyset_005flineno-441"><code>yyset_lineno</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9201. <li><a href="#index-yyset_005fout-440"><code>yyset_out</code></a>: <a href="#Reentrant-Functions">Reentrant Functions</a></li>
  9202. <li><a href="#index-yytables_005fdestroy-473"><code>yytables_destroy</code></a>: <a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a></li>
  9203. <li><a href="#index-yytables_005ffload-472"><code>yytables_fload</code></a>: <a href="#Loading-and-Unloading-Serialized-Tables">Loading and Unloading Serialized Tables</a></li>
  9204. <li><a href="#index-YYText-_0028C_002b_002b-only_0029-394"><code>YYText (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9205.    </ul><div class="node">
  9206. <p><hr>
  9207. <a name="Index-of-Variables"></a>
  9208. Next: <a rel="next" accesskey="n" href="#Index-of-Data-Types">Index of Data Types</a>,
  9209. Previous: <a rel="previous" accesskey="p" href="#Index-of-Functions-and-Macros">Index of Functions and Macros</a>,
  9210. Up: <a rel="up" accesskey="u" href="#Indices">Indices</a>
  9211.  
  9212. </div>
  9213.  
  9214. <h3 class="unnumberedsec">Index of Variables</h3>
  9215.  
  9216. <p>This is an index of variables, constants, and preprocessor macros
  9217. that expand to variables or constants.
  9218.  
  9219.  
  9220.  
  9221. <ul class="index-vr" compact>
  9222. <li><a href="#index-INITIAL-169"><code>INITIAL</code></a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9223. <li><a href="#index-YY_005fCURRENT_005fBUFFER-238"><code>YY_CURRENT_BUFFER</code></a>: <a href="#User-Values">User Values</a></li>
  9224. <li><a href="#index-YY_005fEND_005fOF_005fBUFFER_005fCHAR-212"><code>YY_END_OF_BUFFER_CHAR</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9225. <li><a href="#index-YY_005fNUM_005fRULES-220"><code>YY_NUM_RULES</code></a>: <a href="#Misc-Macros">Misc Macros</a></li>
  9226. <li><a href="#index-YY_005fSTART-239"><code>YY_START</code></a>: <a href="#User-Values">User Values</a></li>
  9227. <li><a href="#index-YY_005fSTART-178"><code>YY_START</code></a>: <a href="#Start-Conditions">Start Conditions</a></li>
  9228. <li><a href="#index-yyextra-428"><code>yyextra</code></a>: <a href="#Extra-Data">Extra Data</a></li>
  9229. <li><a href="#index-yyin-235"><code>yyin</code></a>: <a href="#User-Values">User Values</a></li>
  9230. <li><a href="#index-yyleng-234"><code>yyleng</code></a>: <a href="#User-Values">User Values</a></li>
  9231. <li><a href="#index-yylloc-492"><code>yylloc</code></a>: <a href="#Bison-Bridge">Bison Bridge</a></li>
  9232. <li><a href="#index-YYLMAX-232"><code>YYLMAX</code></a>: <a href="#User-Values">User Values</a></li>
  9233. <li><a href="#index-yylval-491"><code>yylval</code></a>: <a href="#Bison-Bridge">Bison Bridge</a></li>
  9234. <li><a href="#index-yylval_002c-with-yacc-241"><code>yylval, with yacc</code></a>: <a href="#Yacc">Yacc</a></li>
  9235. <li><a href="#index-yyout-237"><code>yyout</code></a>: <a href="#User-Values">User Values</a></li>
  9236. <li><a href="#index-yyscanner-_0028reentrant-only_0029-419"><code>yyscanner (reentrant only)</code></a>: <a href="#Extra-Reentrant-Argument">Extra Reentrant Argument</a></li>
  9237. <li><a href="#index-yytext-229"><code>yytext</code></a>: <a href="#User-Values">User Values</a></li>
  9238. <li><a href="#index-yytext-93"><code>yytext</code></a>: <a href="#Matching">Matching</a></li>
  9239.    </ul><div class="node">
  9240. <p><hr>
  9241. <a name="Index-of-Data-Types"></a>
  9242. Next: <a rel="next" accesskey="n" href="#Index-of-Hooks">Index of Hooks</a>,
  9243. Previous: <a rel="previous" accesskey="p" href="#Index-of-Variables">Index of Variables</a>,
  9244. Up: <a rel="up" accesskey="u" href="#Indices">Indices</a>
  9245.  
  9246. </div>
  9247.  
  9248. <h3 class="unnumberedsec">Index of Data Types</h3>
  9249.  
  9250.  
  9251.  
  9252. <ul class="index-tp" compact>
  9253. <li><a href="#index-FlexLexer-_0028C_002b_002b-only_0029-400"><code>FlexLexer (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9254. <li><a href="#index-YY_005fBUFFER_005fSTATE-191"><code>YY_BUFFER_STATE</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9255. <li><a href="#index-YY_005fEXTRA_005fTYPE-_0028reentrant-only_0029-429"><code>YY_EXTRA_TYPE (reentrant only)</code></a>: <a href="#Extra-Data">Extra Data</a></li>
  9256. <li><a href="#index-yy_005fsize_005ft-214"><code>yy_size_t</code></a>: <a href="#Multiple-Input-Buffers">Multiple Input Buffers</a></li>
  9257. <li><a href="#index-yyFlexLexer-_0028C_002b_002b-only_0029-399"><code>yyFlexLexer (C++ only)</code></a>: <a href="#Cxx">Cxx</a></li>
  9258. <li><a href="#index-YYLTYPE-493"><code>YYLTYPE</code></a>: <a href="#Bison-Bridge">Bison Bridge</a></li>
  9259. <li><a href="#index-yyscan_005ft-_0028reentrant-only_0029-433"><code>yyscan_t (reentrant only)</code></a>: <a href="#About-yyscan_005ft">About yyscan_t</a></li>
  9260. <li><a href="#index-YYSTYPE-494"><code>YYSTYPE</code></a>: <a href="#Bison-Bridge">Bison Bridge</a></li>
  9261.    </ul><div class="node">
  9262. <p><hr>
  9263. <a name="Index-of-Hooks"></a>
  9264. Next: <a rel="next" accesskey="n" href="#Index-of-Scanner-Options">Index of Scanner Options</a>,
  9265. Previous: <a rel="previous" accesskey="p" href="#Index-of-Data-Types">Index of Data Types</a>,
  9266. Up: <a rel="up" accesskey="u" href="#Indices">Indices</a>
  9267.  
  9268. </div>
  9269.  
  9270. <h3 class="unnumberedsec">Index of Hooks</h3>
  9271.  
  9272. <p>This is an index of "hooks" that the user may define. These hooks typically  correspond
  9273. to specific locations in the generated scanner, and may be used to insert arbitrary code.
  9274.  
  9275.  
  9276.  
  9277. <ul class="index-hk" compact>
  9278. <li><a href="#index-YY_005fBREAK-228">YY_BREAK</a>: <a href="#Misc-Macros">Misc Macros</a></li>
  9279. <li><a href="#index-YY_005fUSER_005fACTION-218">YY_USER_ACTION</a>: <a href="#Misc-Macros">Misc Macros</a></li>
  9280. <li><a href="#index-YY_005fUSER_005fINIT-221">YY_USER_INIT</a>: <a href="#Misc-Macros">Misc Macros</a></li>
  9281.    </ul><div class="node">
  9282. <p><hr>
  9283. <a name="Index-of-Scanner-Options"></a>
  9284. Previous: <a rel="previous" accesskey="p" href="#Index-of-Hooks">Index of Hooks</a>,
  9285. Up: <a rel="up" accesskey="u" href="#Indices">Indices</a>
  9286.  
  9287. </div>
  9288.  
  9289. <h3 class="unnumberedsec">Index of Scanner Options</h3>
  9290.  
  9291.  
  9292.  
  9293. <ul class="index-op" compact>
  9294. <li><a href="#index-g_t_002d_002b-307">-+</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9295. <li><a href="#index-g_t_002d_002d_002d7bit-272">—7bit</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9296. <li><a href="#index-g_t_002d_002d_002d8bit-275">—8bit</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9297. <li><a href="#index-g_t_002d_002d_002dalign-325">—align</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9298. <li><a href="#index-g_t_002d_002d_002dalways_002dinteractive-279">—always-interactive</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9299. <li><a href="#index-g_t_002d_002d_002darray-310">—array</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9300. <li><a href="#index-g_t_002d_002d_002dbackup-345">—backup</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9301. <li><a href="#index-g_t_002d_002d_002dbatch-266">—batch</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9302. <li><a href="#index-g_t_002d_002d_002dbison_002dbridge-297">—bison-bridge</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9303. <li><a href="#index-g_t_002d_002d_002dbison_002dlocations-299">—bison-locations</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9304. <li><a href="#index-g_t_002d_002d_002dc_002b_002b-308">—c++</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9305. <li><a href="#index-g_t_002d_002d_002dcase_002dinsensitive-260">—case-insensitive</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9306. <li><a href="#index-g_t_002d_002d_002ddebug-348">—debug</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9307. <li><a href="#index-g_t_002d_002d_002ddefault-277">—default</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9308. <li><a href="#index-g_t_002d_002d_002decs-328">—ecs</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9309. <li><a href="#index-g_t_002d_002d_002dfast-342">—fast</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9310. <li><a href="#index-g_t_002d_002d_002dfull-339">—full</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9311. <li><a href="#index-g_t_002d_002d_002dheader_002dfile-246">—header-file</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9312. <li><a href="#index-g_t_002d_002d_002dhelp-369">—help</a>: <a href="#Miscellaneous-Options">Miscellaneous Options</a></li>
  9313. <li><a href="#index-g_t_002d_002d_002dinteractive-269">—interactive</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9314. <li><a href="#index-g_t_002d_002d_002dlex_002dcompat-263">—lex-compat</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9315. <li><a href="#index-g_t_002d_002d_002dmain-317">—main</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9316. <li><a href="#index-g_t_002d_002d_002dmeta_002decs-333">—meta-ecs</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9317. <li><a href="#index-g_t_002d_002d_002dnever_002dinteractive-281">—never-interactive</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9318. <li><a href="#index-g_t_002d_002d_002dnodefault-354">—nodefault</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9319. <li><a href="#index-g_t_002d_002d_002dnoline-302">—noline</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9320. <li><a href="#index-g_t_002d_002d_002dnounistd-319">—nounistd</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9321. <li><a href="#index-g_t_002d_002d_002dnowarn-360">—nowarn</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9322. <li><a href="#index-g_t_002d_002d_002doption_002dansi_002ddefinitions-293">—option-ansi-definitions</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9323. <li><a href="#index-g_t_002d_002d_002doption_002dansi_002dprototypes-295">—option-ansi-prototypes</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9324. <li><a href="#index-g_t_002d_002d_002doutfile-249">—outfile</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9325. <li><a href="#index-g_t_002d_002d_002dperf_002dreport-351">—perf-report</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9326. <li><a href="#index-g_t_002d_002d_002dpointer-312">—pointer</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9327. <li><a href="#index-g_t_002d_002d_002dposix-283">—posix</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9328. <li><a href="#index-g_t_002d_002d_002dprefix-315">—prefix</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9329. <li><a href="#index-g_t_002d_002d_002dread-336">—read</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9330. <li><a href="#index-g_t_002d_002d_002dreentrant-305">—reentrant</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9331. <li><a href="#index-g_t_002d_002d_002dskel-254">—skel</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9332. <li><a href="#index-g_t_002d_002d_002dstack-285">—stack</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9333. <li><a href="#index-g_t_002d_002d_002dstdinit-287">—stdinit</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9334. <li><a href="#index-g_t_002d_002d_002dstdout-252">—stdout</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9335. <li><a href="#index-g_t_002d_002d_002dtables_002dfile-255">—tables-file</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9336. <li><a href="#index-g_t_002d_002d_002dtables_002dverify-257">—tables-verify</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9337. <li><a href="#index-g_t_002d_002d_002dtrace-357">—trace</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9338. <li><a href="#index-g_t_002d_002d_002dverbose-363">—verbose</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9339. <li><a href="#index-g_t_002d_002d_002dversion-372">—version</a>: <a href="#Miscellaneous-Options">Miscellaneous Options</a></li>
  9340. <li><a href="#index-g_t_002d_002d_002dwarn-365">—warn</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9341. <li><a href="#index-g_t_002d_002d_002dyyclass-321">—yyclass</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9342. <li><a href="#index-g_t_002d_002d_002dyylineno-289">—yylineno</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9343. <li><a href="#index-g_t_002d_002d_002dyywrap-291">—yywrap</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9344. <li><a href="#index-g_t_002d7-271">-7</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9345. <li><a href="#index-g_t_002d8-274">-8</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9346. <li><a href="#index-g_t_002db-344">-b</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9347. <li><a href="#index-g_t_002dB-265">-B</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9348. <li><a href="#index-g_t_002dc-367">-c</a>: <a href="#Miscellaneous-Options">Miscellaneous Options</a></li>
  9349. <li><a href="#index-g_t_002dC-323">-C</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9350. <li><a href="#index-g_t_002dCa-324">-Ca</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9351. <li><a href="#index-g_t_002dCe-327">-Ce</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9352. <li><a href="#index-g_t_002dCF-331">-CF</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9353. <li><a href="#index-g_t_002dCf-330">-Cf</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9354. <li><a href="#index-g_t_002dCm-332">-Cm</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9355. <li><a href="#index-g_t_002dCr-335">-Cr</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9356. <li><a href="#index-g_t_002dd-347">-d</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9357. <li><a href="#index-g_t_002dF-341">-F</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9358. <li><a href="#index-g_t_002df-338">-f</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9359. <li><a href="#index-g_t_002dh-368">-h</a>: <a href="#Miscellaneous-Options">Miscellaneous Options</a></li>
  9360. <li><a href="#index-g_t_002dI-268">-I</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9361. <li><a href="#index-g_t_002di-259">-i</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9362. <li><a href="#index-g_t_002dL-301">-L</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9363. <li><a href="#index-g_t_002dl-262">-l</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9364. <li><a href="#index-g_t_002dn-370">-n</a>: <a href="#Miscellaneous-Options">Miscellaneous Options</a></li>
  9365. <li><a href="#index-g_t_002do-248">-o</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9366. <li><a href="#index-g_t_002dp-350">-p</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9367. <li><a href="#index-g_t_002dP-314">-P</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9368. <li><a href="#index-g_t_002dR-304">-R</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9369. <li><a href="#index-g_t_002ds-353">-s</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9370. <li><a href="#index-g_t_002dT-356">-T</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9371. <li><a href="#index-g_t_002dt-251">-t</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9372. <li><a href="#index-g_t_002dV-371">-V</a>: <a href="#Miscellaneous-Options">Miscellaneous Options</a></li>
  9373. <li><a href="#index-g_t_002dv-362">-v</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9374. <li><a href="#index-g_t_002dw-359">-w</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9375. <li><a href="#index-g_t_002dX-282">-X</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9376. <li><a href="#index-g_t7bit-273">7bit</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9377. <li><a href="#index-g_t8bit-276">8bit</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9378. <li><a href="#index-align-326">align</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9379. <li><a href="#index-always_002dinteractive-280">always-interactive</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9380. <li><a href="#index-ansi_002ddefinitions-294">ansi-definitions</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9381. <li><a href="#index-ansi_002dprototypes-296">ansi-prototypes</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9382. <li><a href="#index-array-311">array</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9383. <li><a href="#index-backup-346">backup</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9384. <li><a href="#index-batch-267">batch</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9385. <li><a href="#index-bison_002dbridge-298">bison-bridge</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9386. <li><a href="#index-bison_002dlocations-300">bison-locations</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9387. <li><a href="#index-c_002b_002b-309">c++</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9388. <li><a href="#index-case_002dinsensitive-261">case-insensitive</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9389. <li><a href="#index-debug-349">debug</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9390. <li><a href="#index-default-278">default</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9391. <li><a href="#index-ecs-329">ecs</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9392. <li><a href="#index-fast-343">fast</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9393. <li><a href="#index-full-340">full</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9394. <li><a href="#index-header_002dfile-247">header-file</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9395. <li><a href="#index-interactive-270">interactive</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9396. <li><a href="#index-lex_002dcompat-264">lex-compat</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9397. <li><a href="#index-main-318">main</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9398. <li><a href="#index-meta_002decs-334">meta-ecs</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9399. <li><a href="#index-nodefault-355">nodefault</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9400. <li><a href="#index-noline-303">noline</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9401. <li><a href="#index-nounistd-320">nounistd</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9402. <li><a href="#index-nowarn-361">nowarn</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9403. <li><a href="#index-noyyalloc-459">noyyalloc</a>: <a href="#Overriding-The-Default-Memory-Management">Overriding The Default Memory Management</a></li>
  9404. <li><a href="#index-outfile-250">outfile</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9405. <li><a href="#index-perf_002dreport-352">perf-report</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9406. <li><a href="#index-pointer-313">pointer</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9407. <li><a href="#index-posix-284">posix</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9408. <li><a href="#index-prefix-316">prefix</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9409. <li><a href="#index-read-337">read</a>: <a href="#Options-for-Scanner-Speed-and-Size">Options for Scanner Speed and Size</a></li>
  9410. <li><a href="#index-reentrant-306">reentrant</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9411. <li><a href="#index-stack-286">stack</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9412. <li><a href="#index-stdinit-288">stdinit</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9413. <li><a href="#index-stdout-253">stdout</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9414. <li><a href="#index-tables_002dfile-256">tables-file</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9415. <li><a href="#index-tables_002dverify-258">tables-verify</a>: <a href="#Options-for-Specifing-Filenames">Options for Specifing Filenames</a></li>
  9416. <li><a href="#index-trace-358">trace</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9417. <li><a href="#index-verbose-364">verbose</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9418. <li><a href="#index-warn-366">warn</a>: <a href="#Debugging-Options">Debugging Options</a></li>
  9419. <li><a href="#index-yyclass-322">yyclass</a>: <a href="#Code_002dLevel-And-API-Options">Code-Level And API Options</a></li>
  9420. <li><a href="#index-yylineno-290">yylineno</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9421. <li><a href="#index-yywrap-292">yywrap</a>: <a href="#Options-Affecting-Scanner-Behavior">Options Affecting Scanner Behavior</a></li>
  9422.    </ul><!-- A vim script to name the faq entries. delete this when faqs are no longer -->
  9423. <!-- named "unnamed-faq-XXX". -->
  9424. <!-- fu! Faq2 () range abort -->
  9425. <!-- let @r=input("Rename to: ") -->
  9426. <!-- exe "%s/" . @w . "/" . @r . "/g" -->
  9427. <!-- normal 'f -->
  9428. <!-- endf -->
  9429. <!-- nnoremap <F5>  1G/@node\s\+unnamed-faq-\d\+<cr>mfww"wy5ezt:call Faq2()<cr> -->
  9430. <div class="footnote">
  9431. <hr>
  9432. <a name="texinfo-footnotes-in-document"></a><h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> Actually,
  9433. <code>yyIN_HEADER</code> is defined before the `<samp><span class="samp">%top</span></samp>' block.</p>
  9434.  
  9435.    <p class="footnote"><small>[<a name="fn-2" href="#fnd-2">2</a>]</small> The
  9436. quantities given here are approximate, and may vary due to host architecture,
  9437. compiler configuration, or due to future enhancements to flex.</p>
  9438.  
  9439.    <p class="footnote"><small>[<a name="fn-3" href="#fnd-3">3</a>]</small> It
  9440. is not necessary to override all (or any) of the memory management routines. 
  9441. You may, for example, override <code>yyrealloc</code>, but not <code>yyfree</code> or
  9442. <code>yyalloc</code>.</p>
  9443.  
  9444.    <p class="footnote"><small>[<a name="fn-4" href="#fnd-4">4</a>]</small> GNU <samp><span class="command">make</span></samp> and GNU <samp><span class="command">automake</span></samp> are two such
  9445. programs that provide implicit rules for flex-generated scanners.</p>
  9446.  
  9447.    <p class="footnote"><small>[<a name="fn-5" href="#fnd-5">5</a>]</small> This example also applies to yacc parsers.</p>
  9448.  
  9449.    <p class="footnote"><small>[<a name="fn-6" href="#fnd-6">6</a>]</small> The features described here are
  9450. purely optional, and are by no means the only way to use flex with bison. 
  9451. We merely provide some glue to ease development of your parser-scanner pair.</p>
  9452.  
  9453.    <p class="footnote"><small>[<a name="fn-7" href="#fnd-7">7</a>]</small> The use of m4 is subject to change in
  9454. future revisions of flex.</p>
  9455.  
  9456.    <p><hr></div>
  9457.  
  9458. </body></html>
  9459.  
  9460.